Safe ways to do things in bash
github.com
Safe ways to do things in bash
1–10 of 255 posts
Re: Safe ways to do things in bash
#2if you are on busybox with ash none of this is helping (except shellcheck which is great).
Re: Safe ways to do things in bash
#3[1] https://github.com/koalaman/shellcheck
[2] https://github.com/SublimeLinter/SublimeLinter-shellcheck
Re: Safe ways to do things in bash
#4If 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).
Re: Safe ways to do things in bash
#5Nope. 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.
Re: Safe ways to do things in bash
#6If 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).
I still use bash a lot though, it's just handy, with lua and python you will have to deal with various of its packages sooner or later, for bash it is just one binary and you get 90% scripting tasks covered, zero dependencies.
Re: Safe ways to do things in bash
#7If you need safety don't use Bash.
Re: Safe ways to do things in bash
#8> 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 variants into the "better" form.
I prefer the "${bracey}" form for all variable usage. Yes it's marginally more verbose but it has the advantage of being consistent, easier on the eyes due to less overall quoting when part of full string interpolation[1], and cleaner diffs[2] as converting "${foo}" to "${foo}-bar" only leads to word-diff of "-bar".
[1]: "${foo} bar baz" v.s. "$foo"" bar baz"
[2]: You are source controlling your shell scripts right?
Re: Safe ways to do things in bash
#9Highly recommend shellcheck[1]. There is a SublimeLinter plugin[2] that automatically checks your shell scripts as you code them. It generally makes best practice suggestions including quoting. [1] https://github.com/koalaman/shellcheck [2] https://github.com/SublimeLinter/SublimeLinter-shellcheck
Re: Safe ways to do things in bash
#10If 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).
Python (the language with the most community traction to replace bash for scripts) is a royal pain to use for this without libraries that are present in no default system, and even with those it often ends up being more verbose than it should.