News for hackers. Stuff that matters.
Re:Shellcheck finds bugs in your shell scripts by mr-wendel on Thursday November 23, 2023 @03:51PM (#38397388)
Some tips of my own:

- It's almost always preferable to put `-u` (nounset) in your shebang to cause using undeclared variables to be an error. The only exception I typically run across is expansion of arrays using "${arr[@]}" syntax -- if the array is empty, this is considered unbound.

- You can use `-n` (noexec) as a poor-man's attempt at a dry-run, as this will prevent execution of commands.

- Also handy is `-e` (errexit), but you must take care to observe that essentially, this only causes "naked" commands that fail to cause an exit. Personally, I prefer to avoid this and append `|| fail "..."` to commands liberally.

Re:Shellcheck finds bugs in your shell scripts by augusto-moura on Thursday November 23, 2023 @04:24PM (#38397746)
The problem with "${arr[@]}" only exists on bash 3 and before, since bash 4, [@] will never throw unbound variables even in cases where the variable is truly undefined. This is still a problem however, because macOS, to this day, still installs bash v3 by default and doesn't update it automatically (absolute madness, the last release of bash 3 it's from 20 years ago!).

In any case, you can workaround expanding empty arrays throwing unbound by using the ${var+alter} expansion

  echo "${arr+${arr[@]}}"
Re:Shellcheck finds bugs in your shell scripts by mmsc on Thursday November 23, 2023 @04:33PM (#38397820)

  The problem with "${arr[@]}" only exists on bash 3 and before, since bash 4
4.4 fixed it:

  $ bash --version
  GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
  $ declare -A arr
  $ set -u
  $ "${arr[@]}"
  -bash: arr[@]: unbound variable
Re:Shellcheck finds bugs in your shell scripts by augusto-moura on Thursday November 23, 2023 @05:29PM (#38398352)
Ah, that's true, I couldn't recall which version fixed it. Usually I assume v4 because any other distro automatically updates to the latest v4 version or latest version af all.

macOS is the only one out there missing on this. The other big feature that was only added after bash 3 and is missing on mac is associative arrays

Re:Shellcheck finds bugs in your shell scripts by extraduder_ire on Friday November 24, 2023 @07:07AM (#38403126)
> macOS, to this day, still installs bash v3 by default and doesn't update it automatically (absolute madness, the last release of bash 3 it's from 20 years ago!).

GPL3, buddy. You can use brew to install a newer version and somewhat hide the old one.

Re:Shellcheck finds bugs in your shell scripts by Calzifer on Thursday November 23, 2023 @04:15PM (#38397645)
> to put `-u` (nounset) in your shebang

Any particular reason why in the shebang instead of set -u?

> The only exception I typically run across is expansion of arrays using "${arr[@]}" syntax

In Bash? Works for me. Edit: another comment mentions it as well. Seem to behave better in newer versions of Bash and only problematic in <= 4.3 https://news.ycombinator.com/item?id=38397241

  $ bash -uc 'unset x; echo "=> ${x[@]}"'
  =>
  $ bash -uc 'x=(); echo "=> ${x[@]}"'
  =>
  $ bash -uc 'x=(); echo "=> ${x[0]}"'
  bash: x[0]: unbound variable
Zsh does not like the first example but both should support:

  $ bash -uc 'unset x; echo "=> ${x[@]:-null}"'
  => null

> Also handy is `-e` (errexit),

It is unfortunately very confusion with functions. Made me like it less over the years.

Re:Shellcheck finds bugs in your shell scripts by mr-wendel on Thursday November 23, 2023 @04:31PM (#38397797)
Using the shebang just helps highlight the fact that the rule is in use globally, but otherwise has no advantage to using `set -u`.

The clarifications on `-u` and arrays are useful. I'm definitely used to assuming newer (... non-ancient?) versions of Bash are what is available.

Re:Shellcheck finds bugs in your shell scripts by Xophmeister on Thursday November 23, 2023 @05:24PM (#38398312)
Using `set -u` is more portable. If your shebang is `/usr/bin/env bash`, which it probably should be, then you can't add additional command line arguments in Linux with older coreutils. macOS supports additional arguments, regardless, and in Linux, coreutils 8.30 added the `-S` option to `env` to get around this problem.
Re:Shellcheck finds bugs in your shell scripts by anamexis on Thursday November 23, 2023 @06:27PM (#38398937)
Regarding the `-e` issue, this is well handled by `-o pipefail`, which as of last year is part of POSIX.
Re:Shellcheck finds bugs in your shell scripts by extraduder_ire on Friday November 24, 2023 @07:12AM (#38403170)
I find that I usually have to use traps when I put use options to terminate the script early, mostly when I have some files to clean up.

"trap" is a great scripting feature that doesn't get talked about enough, IME.

Re:Shellcheck finds bugs in your shell scripts by bruce_one on Monday November 27, 2023 @05:46AM (#38430756)
One common suggestion is to not set Bash/shell options in the shebang because if someone runs the script with the interpreter explicitly (e.g. `bash my_script.sh`, rather than `./my_script.sh` (which, ime, is quite common as many people to seem to do it to avoid setting the execute bit, sometimes due to a lack of understanding ime)) then the options won't apply - which isn't fun...

Using `set` (e.g. `set -euo pipefail` as the first line after the shebang) is a common suggestion due to this, and similarly it helps with the `#!/usr/bin/env bash` case where the shebang can't handle additional arguments :-)