https://utcc.utoronto.ca/~cks/space/blog/programming/BashGoodSetEReports Chris Siebenmann :: CSpace >> blog >> programming >> BashGoodSetEReports Welcome, guest. Getting decent error reports in Bash when you're using 'set -e' July 23, 2025 Suppose that you have a shell script that's not necessarily complex but is at least long. For reliability, you use 'set -e' so that the script will immediately stop on any unexpected errors from commands, and sometimes this happens. Since this isn't supposed to happen, it would be nice to print some useful information about what went wrong, such as where it happened, what the failing command's exit status was, and what the command was. The good news is that if you're willing to make your script specifically a Bash script, you can do this quite easily. The Bash trick you need is: trap 'echo "Exit status $? at line $LINENO from: $BASH_COMMAND"' ERR This uses three Bash features: the special '$LINENO' and '$BASH_COMMAND' environment variables (which have the command executed just before the trap and the line number), and the special 'ERR' Bash 'trap' condition that causes your 'trap' statement to be invoked right when 'set -e' is causing your script to fail and exit. Using 'ERR' instead of 'EXIT' (or '0' if you're a traditionalist like me) is necessary in order to get the correct line number in Bash. If you switch this to 'trap ... EXIT', the line number that Bash will report is the line that the 'trap' was defined on, not the line that the failing command is on (although the command being executed remains the same). This makes a certain amount of sense from the right angle; the shell is currently on that line as it's exiting. As far as I know, no other version of the Bourne shell can do all of this. The OpenBSD version of /bin/sh has a '$LINENO' variable and 'trap ... 0' preserves its value (instead of resetting it to the line of the 'trap'), but it has no access to the current command. The FreeBSD version of /bin/sh resets '$LINENO' to the line of your 'trap ... 0', so the best you can do is report the exit status. Dash, the Ubuntu 24.04 default /bin/sh, doesn't have '$LINENO', effectively putting you in the same situation as FreeBSD. (On Fedora, /bin/sh is Bash, and the Fedora version of Bash supports all of 'trap .. ERR', $LINENO, and $BASH_COMMAND even when invoked as '#!/bin/sh' by your script. You probably shouldn't count on this; if you want Bash, use '#!/bin/bash'.) (One comment.) Written on 23 July 2025. NFS v4 delegations on a Linux Sending drawing commands to your << NFS server can act as mandatory display server versus sending >> locks images These are my WanderingThoughts (About the blog) Full index of entries Recent comments This is part of CSpace, and is written by ChrisSiebenmann. Mastodon: @cks [S:Twitter:S] @thatcks * * * Categories: links, linux, programming, python, snark, solaris, spam, sysadmin, tech, unix, web Also: (Sub)topics This is a DWiki. GettingAround (Help) Search: [ ] --------------------------------------------------------------------- Page tools: View Source, Add Comment. Search: [ ] Login: [ ] Password: [ ] [Login] Atom Syndication: Recent Comments. --------------------------------------------------------------------- Last modified: Wed Jul 23 22:57:21 2025 This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.