Live data from Hacker News

Safe ways to do things in bash

github.com

1–10 of 255 posts

Re: Safe ways to do things in bash

#2
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).

Re: Safe ways to do things in bash

#3
Highly 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

#4
post #2

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).

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

#5
> 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.

Re: Safe ways to do things in bash

#6
post #4
post #2

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).

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.

if you need arrays use mksh - https://www.mirbsd.org/mksh.htm - if you need complex scripting with small footprint and standard compliance avoid bash and use mksh.

Re: Safe ways to do things in bash

#8
From 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 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

#9

Highly 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

Not only does it make suggestions, almost all of the 'error codes' have extensive documentation on why something is wrong and often multiple alternative solutions for each use case (eg: https://github.com/koalaman/shellcheck/wiki/SC2086). I learned more bash from Shellcheck than all tutorials and references combined.

Re: Safe ways to do things in bash

#10
post #2

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).

Sometimes you just want some 30-50 lines of piping a few commands and a couple of conditionals.

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.

Post reply on HN