Skip to main content
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 ...
S0AndS0's user avatar
  • 466
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: ...
unpythonic's user avatar
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 ...
Mark Plotnick's user avatar
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 ...
Stéphane Chazelas's user avatar
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–...
Gilles 'SO- stop being evil''s user avatar
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 ...
Kusalananda's user avatar
  • 356k
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 ...
muru's user avatar
  • 77.9k
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 ...
Kusalananda's user avatar
  • 356k
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 ...
Kusalananda's user avatar
  • 356k
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" ...
Tom Hale's user avatar
  • 33.3k
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, ...
aviro's user avatar
  • 6,945
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: ...
philipp2100's user avatar
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() ...
Vojtech Vitek's user avatar
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 ...
Jeff Schaller's user avatar
  • 68.8k
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 ...
RichVel's user avatar
  • 209
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, ...
DopeGhoti's user avatar
  • 79.2k
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 ...
Arkadiusz Drabczyk's user avatar
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 ...
Tom Hale's user avatar
  • 33.3k
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 ...
Jonathan Mayer's user avatar
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 ...
Kusalananda's user avatar
  • 356k
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 ...
ivan's user avatar
  • 1,938
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 ...
ilkkachu's user avatar
  • 148k
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 ...
xenoid's user avatar
  • 9,338
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 ...
Reinstate Monica's user avatar
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 ...
Kusalananda's user avatar
  • 356k

Only top scored, non community-wiki answers of a minimum length are eligible