Live data from Hacker News

Shell script best practices, from a decade of scripting things

sharats.me

41–50 of 500 posts

Re: Shell script best practices, from a decade of scripting things

#41

> Use bash. Using zsh or fish or any other, will make it hard for others to understand / collaborate. Among all shells, bash strikes a good balance between portability and DX. I think fish is quite a bit different in terms of syntax and semantics (I'm not very familiar with it), but zsh is essentially the same as bash except without most of the needless footguns and awkwardness. zsh also has many more advanced featur…

If I had the choice of using zsh, then most likely I would had the choice to use python.

Re: Shell script best practices, from a decade of scripting things

#42
I'm not convinced about having shell scripts end with ".sh" as you may be writing a simple command style script and shouldn't have to know or worry about what language it's using.

I'm a fan of using BASH3 boilerplate: https://bash3boilerplate.sh/

It's standalone, so you just start a script using it as a template and delete bits that you don't want. To my mind, the best feature is having consistent logging functions, so you're encouraged to put in lots of debug commands to output variable contents and when you change LOG_LEVEL, all the extraneous info doesn't get shown so there's no need to remove debug statements at all.

The other advantage is the option parsing, although I don't like the way that options have to have a short option (e.g. -a) - I'd prefer to just use long options.

Re: Shell script best practices, from a decade of scripting things

#44
post #35

See also: * safe ways to do things in bash: https://github.com/anordal/shellharden/blob/master/how_to_do... * better scripting: https://robertmuth.blogspot.in/2012/08/better-bash-scripting... * robust scripting: https://www.davidpashley.com/articles/writing-robust-shell-s...

I can highly recommend Greg's wiki/BASH faq: https://mywiki.wooledge.org/BashFAQ

Now when I'm processing files with BASH, I nearly always end up copying stuff from there as it just bypasses common errors such as not handling whitespace or filenames that contain line breaks.

Re: Shell script best practices, from a decade of scripting things

#45
> For copy-paste: if [[ -n "${TRACE-}" ]]; then set -o xtrace; fi

> People can now enable debug mode, by running your script as TRACE=1 ./script.sh instead of ./script.sh.

The above "if" condition will set xtrace even when user explicitly disables trace by setting TRACE=0.

A correct way of doing this would be:

    if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi

Re: Shell script best practices, from a decade of scripting things

#46
Use the shell only if your script is mostly about calling other programs and filtering and redirecting their output. That's what the syntax of these languages is optimised for. As soon as you need any data manipulation (i.e. arrays, computation, etc.) it becomes a pain and Python is the much better fit.

Re: Shell script best practices, from a decade of scripting things

#48
post #16

> If appropriate, change to the script’s directory close to the start of the script. > And it’s usually always appropriate. I wouldn't think so. You don't know where your script will be called from, and many times the parameters to the script are file paths, which are relative to the caller's path. So you usually don't want to do it. I collected many tips&tricks from my experience with shell scripts that you may also…

I agree. I make an effort to not change directory wherever possible and if a change is needed, do it in a subshell and just for the command that needs it (hardly any commands actually need it, anyway).

Edit: just had a quick look at your recommended link and spotted a "mistake" in 4.7 - using "read" without "-r" would get caught out by shellcheck.

Re: Shell script best practices, from a decade of scripting things

#50

In my experience, the best practice is to implement all the non-trivial logic in the actual program or a Python script, and use shell script only for very straight-forward stuff like handling command-line arguments, environment variables and paths.

Best practice for shell scripting: don't.
Post reply on HN