If you reached a point where you need to require bash and not a posix shell and need to enforce these rules just use python or lua if you can or some scheme or whatever else... it's not worth the wasted time hunting bash cruft. if you are on busybox with ash none of this is helping (except shellcheck which is great).
Safe ways to do things in bash
51–60 of 255 posts
Re: Safe ways to do things in bash
#52Re: Safe ways to do things in bash
#53Re: Safe ways to do things in bash
#54It's written by the same people who wrote the python ssh scripting framework fabric.
Works great!!
Re: Safe ways to do things in bash
#55> POSIX mandates /bin/sh Nope. On the contrary, it says: Applications should note that the standard PATH to the shell cannot be assumed to be either /bin/sh or /usr/bin/sh Source: http://pubs.opengroup.org/onlinepubs/009695399/utilities/sh.... A pedantically-compliant shell script should not have shebang at all.
What's wrong with `#!/usr/bin/env sh`?
Android, motherfuckers.
Re: Safe ways to do things in bash
#56Earlier quoted context omitted.
What's wrong with `#!/usr/bin/env sh`?
I've worked on systems where env was installed as /bin/env and not as /usr/bin/env . (I think it was SunOS 4.) For that matter, under Termux on Android it's /data/data/com.termux/files/usr/bin/env (but termux has a hack to make normal shebangs work).
Re: Safe ways to do things in bash
#57Backticks are very error prone $(cat foo.txt) is much more explicit and visually clearer for command substitution.
Re: Safe ways to do things in bash
#58From the article: > Should I use curly braces? Bad: some_command $arg1 $arg2 $arg3 Extra bad (cargo culting unnecessary braces): some_command ${arg1} ${arg2} ${arg3} Correct: some_command "${arg1}" "${arg2}" "${arg3}" Better: some_command "$arg1" "$arg2" "$arg3" > In the "extra bad" and "correct" examples, braces compete with quotes under the limits of tolerable verbosity. > Shellharden will rewrite all these variant…
> You are source controlling your shell scripts right? My personal heuristic for shell scripts are that if I care enough about them to put them under source control, they shouldn't be a shell script.
Re: Safe ways to do things in bash
#59For example:
parent_dir = "$(basename $dir)"
How to quote $dir here? What if it contains spaces or other special characters?That's what I dislike about Bash.
Also, always start your scripts with
set -e
This prevents script from running after error, without any messages though.Also, I always make mistakes when using [ and [[.
Re: Safe ways to do things in bash
#60 $ cat - > foo
#!/bin/bash
a="BCD"
[[ $a =~ .C. ]] && echo 1
[[ $a =~ ".C." ]] && echo 2
^D
$ bash foo
1
$