18
votes
Trap, ERR, and echoing the error line
Is it possible to get what line the ERR signal was sent from?
Yes, LINENO and BASH_LINENO variables are supper useful for getting the line of failure and the lines that lead up to it.
Or ...
17
votes
Trap, ERR, and echoing the error line
I really like the answer given by @Mat above. Building on this, I wrote a little helper which gives a bit more context for the error:
We can inspect the script for the line which caused the failure:
...
12
votes
Trap 'Ctrl + c' for bash script but not for process open in this script
You can reset a trap to its default by giving the trap command - as its action argument. If you do this in a subshell, it won't affect the trap in the parent shell. In your script, you can do this ...
12
votes
Accepted
Why does `trap` passthough zero instead of the signal the process was killed with?
$? contains the exit status of the last command that was run and waited for. You'll find that in:
$ bash -c 'trap "echo \$?" INT; sleep 10; exit'
^C130
130 was reported because both sleep ...
11
votes
Accepted
How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something
On entry to the EXIT trap, $? contains the exit status. That's the same value you'd find as $? after calling this script in another shell: either the argument passed to exit (truncated to the range 0–...
11
votes
EXIT Trap with POSIX
In a strictly POSIX shell, the EXIT trap is evaluated before the shell exits due to executing exit or due to executing the last command in a script. It is not executed if the shell exits due to a ...
11
votes
Accepted
What is the use of declare with option -t
declare -t foo sets the trace attribute on the variable foo (which has no special effect anyway). You need to use -f to set it on the function:
declare -ft foo
With your script modified to use -f, I ...
10
votes
Accepted
Wait for signal
No.
wait is exclusively used in a parent process to wait for the termination of a child process (and to access its exit status).
Furthermore, no process may trap the KILL signal (the original ...
10
votes
Accepted
Use bash EXIT trap to confirm or cancel ctrl+d
set -o ignoreeof
This will cause the interactive shell to ignore EOF (Ctrl+D).
The bash shell will print
Use "exit" to leave the shell.
if you press Ctrl+D.
You may also set the shell variable ...
9
votes
Keep exit codes when trapping SIGINT and similar?
Any usual signal exit code will be available in $? upon entry to the trap handler:
sig_handler() {
exit_status=$? # Eg 130 for SIGINT, 128 + (2 == SIGINT)
echo "Doing signal-specific up"
...
9
votes
How can I make a script echo something when it is paused?
you should use SIGTSTP instead of SIGSTOP. See here:
The SIGSTOP signal stops the process. It cannot be handled, ignored,
or blocked.
The SIGTSTP signal is an interactive stop signal. Unlike SIGSTOP, ...
8
votes
Keep exit codes when trapping SIGINT and similar?
Just return some error code isn't enough to simulate an exit by SIGINT.
I'm surprised that nobody mentioned this so far.
Further reading: https://www.cons.org/cracauer/sigint.html
The proper way is:
...
8
votes
"trap ... INT TERM EXIT" really necessary?
This is how you can make the Bash script report its return code $?, while being able to catch the SIGINT and SIGTERM signals. I find this very useful for scripts running in a CI/CD pipeline:
notify() ...
8
votes
Accepted
Trap both INT and ERR, but callback got executed multiple times
You're getting both the INT and ERR signals; SIGINT is handed to sleep, who exits with a non-zero return code. The non-zero return code then triggers the trap for SIGERR.
If a sigspec is ERR, the ...
7
votes
Trap, ERR, and echoing the error line
Here's another version, inspired by @sanmai and @unpythonic. It shows script lines around the error, with line numbers, and the exit status - using tail & head as that seems simpler than the awk ...
7
votes
Accepted
How to prevent one command from triggering ERR trap?
An ERR trap will not trigger if an error code is immediately "caught", which means that you can use if statements and whatnot without having to flip error trapping on and off all the time. However, ...
7
votes
Accepted
How to cleanup on suspense (ctrl-z) in a Bash script?
Signals handling on Linux and other UNIX-like systems is a very
complex subject with many actors at play: kernel terminal driver,
parent -> child process relation, process groups, controlling
...
6
votes
Silently start task in background
Here's how to do it in both {ba,z}sh.
Using a function helps not needing to save and restore the state of zsh's monitor variable by use of setopt local_options.
# Run the command given by "$@" in ...
6
votes
How to prevent one command from triggering ERR trap?
I find that I often want to avoid ERR trap but I don't want to discard the return code of my function either. So the pattern I use is this:
RC=0
some_function || RC=$?
Then I do whatever I need to ...
6
votes
Accepted
Trap and collect script output, "input file is output file" error?
With the exec, you are redirecting all output of the script to a specific log file.
In your trap, you want to display the contents of the log file using cat. Since all output is also redirected to ...
6
votes
Accepted
Killing background processes started in nix-shell
I've been using something like the following:
./shell.nix:
let
pkgs = import <nixpkgs> { };
in with pkgs; mkShell {
buildInputs = [ glibcLocales postgresql ];
shellHook = ''
export ...
5
votes
Delay termination of script
So, since you don't really want an interrupt, how about disabling it on the terminal level, with stty intr ""? That would make the ^C appear in the input buffer as any ordinary character, and we can ...
5
votes
Accepted
Capture bash function's result and allow it to exit
This is because $(crash) executes crash in a subshell, so the exit applies to the subshell and not to your script.
What is the point of capturing the output in a variable if you won't use it because ...
5
votes
trapped in bash traps (RETURN trap)
I can't find this documented anywhere, but it seems like adding:
trap - RETURN
as the last command in the trap handler, causes the trap to revert to the previous one (bash is keeping a stack of RETURN ...
5
votes
Accepted
Bash redirection weird behavior
The EXIT trap is executed with the redirections that are in place when the trap is called. In your code, calling exit 1 in the main case statement causes the cleanup function to inherit the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
trap × 188bash × 109
signals × 62
shell-script × 45
shell × 33
error-handling × 13
linux × 8
scripting × 7
function × 7
subshell × 7
exit × 7
snmp × 7
zsh × 6
io-redirection × 6
ssh × 5
process × 5
kill × 5
background-process × 5
sigint × 5
ksh × 4
posix × 4
exit-status × 4
interrupt × 4
ubuntu × 3
debugging × 3