Bash offers numerous choices that can be utilized to manage the habits of bash scripts. This submit examines a number of the extra helpful ones and explains easy methods to show which choices are in use and which aren’t.
Exiting when an error happens
If you need a bash script to exit quickly because it encounters an error—any error in any respect—in your scripts, you possibly can add the set -o errexit choice. In case your script incorporates a syntax error, tries to learn a file that doesn’t exist, makes an attempt to append to a file whenever you don’t have permission to take action, or misuses a command not directly, the script will finish abruptly. Right here is an easy instance:
#!/bin/bash set -o errexit tail NoSuchFile echo -n “Enter textual content to be appended> “ learn txt echo $txt >> NoSuchFile
Attempt to run this script, and also you’ll see this:
$ app2file tail: NoSuchFile: No such file or listing
As a result of NoSuchFile doesn’t exist, the script exits, and the immediate for textual content to be appended to it’s by no means run. With out the errexit setting, the script would proceed operating after the “No such file” error and would accumulate any textual content entered after the immediate, create the file, and add the textual content to it.
Tracing a bash script
Utilizing the xtrace choice, each command in a script that’s run will likely be displayed. This feature is particularly helpful if you end up debugging a fancy script. You’ll be able to merely take away the xtrace choice or remark out that line whenever you now not want the command tracing.
#!/bin/bash set -o xtrace for day in Solar Mon Tue Wed Thu Fri Sat do echo $day sleep 2 performed
The output from this script would start like what’s proven beneath. Traces starting with + indicators wouldn’t seem if the xtrace choice weren’t getting used.
$ loop-days-of-week + for day in Solar Mon Tue Wed Thu Fri Sat + echo Solar Solar + sleep 2 + for day in Solar Mon Tue Wed Thu Fri Sat + echo Mon Mon + sleep 2 + for day in Solar Mon Tue Wed Thu Fri Sat + echo Tue Tue …
Stopping at unbound variables
Scripts will typically ignore variables that haven’t been outlined. In case you use the nounset choice as proven within the instance beneath, they are going to exit moderately than proceed operating.
$ cat NoSuchVar #!/bin/bash set -o nounset echo $1 echo $2 $ $ NoSuchVar 1
1 ./NoSuchVar: line 6: $2: unbound variable
Utilizing the quick kind for choices
It’s typically extra useful to set choices in scripts utilizing their full names as this makes the scripts extra readable, however you possibly can elect to make use of their quick kinds. For instance, the xtrace choice might be invoked with the command set -o xtrace as within the instance proven earlier. It will possibly, nevertheless, even be set utilizing the easier command set -x. Lots of the bash choices have single-character names like this that can be utilized within the “set -?” format. This data might be discovered within the bash man web page, however the listing beneath ought to make this simpler.
Choice Brief Type ====== ========== allexport -a braceexpand -B errexit -e errtrace -E functrace -T hashall -h histexpand -H key phrase -k monitor -m noclobber -C noexec -n noglob -f notify -b nounset -u onecmd -t bodily -P privileged -p verbose -v xtrace -x
Show the choices
To generate an inventory of choices accessible with bash and whether or not they’re energetic or not, you should utilize the set -o command with no arguments. I used the set -o | column command on the command line to generate the output beneath and adjusted it frivolously to make the columns line up so it will be simpler to learn for the reason that column alignment was thrown off by the one prolonged choice identify (interactive-comments).
allexport off ignoreeof off nounset off braceexpand on interactive-comments on onecmd off emacs on key phrase off bodily off errexit off monitor on pipefail off errtrace off noclobber off posix off functrace off noexec off privileged off hashall on noglob off verbose off histexpand on nolog off vi off historical past on notify off xtrace off
Be aware that a lot of the bash choices are turned off.
To see which choices are enabled in a script, use the identical set -o command. The script proven beneath will listing the energetic choices as proven above, however can even show the energetic choices in a terse one-letter-per-option format.
#!/bin/bash set -o echo =========================== printf %sn “$-“
Here’s a pattern of its output:
allexport off braceexpand on emacs off errexit off errtrace off functrace off hashall on histexpand off historical past off ignoreeof off interactive-comments on key phrase off monitor off noclobber off noexec off noglob off nolog off notify off nounset off onecmd off bodily off pipefail off posix off privileged off verbose off vi off xtrace off =========================== hB
Be aware that the “h” and “B” on the final line on this output mirror the standing of the hashall (h) and braceexpand (B) choices.
Wrap-Up
There’s much more to study easy methods to use the choices accessible in bash to manage how scripts run. I hope this submit will get you off to a superb begin.
Copyright © 2022 IDG Communications, Inc.