> 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…
Shell script best practices, from a decade of scripting things
41–50 of 500 posts
Re: Shell script best practices, from a decade of scripting things
#42I'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
#43Re: Shell script best practices, from a decade of scripting things
#44See 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...
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> 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; fiRe: Shell script best practices, from a decade of scripting things
#46Re: Shell script best practices, from a decade of scripting things
#47Re: Shell script best practices, from a decade of scripting things
#48> 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…
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
#49Re: Shell script best practices, from a decade of scripting things
#50In 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.