2 ms·
I don't agree with the advice to use arrays and set -u together. Sometimes you just need to get something done, and being pedantic works against you. This adv
by chubot 8y ago
I don't agree with the advice to use arrays and set -u together. Sometimes you just need to get something done, and being pedantic works against you.
This advice only works in bash 4.4, and many common distros are on bash 4.3, like Ubuntu 16.02 LTS. (bash 4.4 was released September 2016.) Because of this bug, the section "how to begin a bash script" is version-specific and awkward IMO.
If you need to process untrusted filenames, use arrays, but otherwise it's probably more trouble than it's worth. [1]
I want my scripts to work on older versions of bash, and I think 'set -u' is important, so I mostly get by without arrays. It's not ideal, but shell is full of compromises.
A workaround is to use ${a[@]+"${a[@]}"}, which avoids the bug in bash 4.3, but that seems too ugly to recommend.
More details in this comment I wrote on the same article: https://lobste.rs/s/4jegyk/how_do_things_safely_bash#c_kmldw6 https://lobste.rs/s/4jegyk/how_do_things_safely_bash#c_kmldw...
[1] Thirteen Incorrect Ways and Two Awkward Ways to Use Arrays http://www.oilshell.org/blog/2016/11/06.html http://www.oilshell.org/blog/2016/11/06.html