Tested for Bash 5.0.2
According to the GNU Bash Reference Manual,
Bash performs the expansion [of a command substitution] by executing [the] command in a subshell environment
According to The Open Group Base Specifications Issue 6:
when a subshell is entered, traps that are not being ignored are set to the default actions.
So when running the following script:
function a {
trap -p EXIT
}
trap "echo 'parent'" EXIT
echo "$(a)"
(a)
trap - EXIT
echo 'exiting'
... i would expect an output of:
exiting
... but instead I get:
trap -- 'echo '\''parent'\''' EXIT
trap -- 'echo '\''parent'\''' EXIT
exiting
... meaning that the function a - eventhough it is being run in a subshell - is seeing the the parent shell's trap commands (via trap -p) but not executing them.
What is going on here?
You'll notice that the traps trigger exactly according to spec. It's just the output from trap that's unexpected.
This is a feature in Bash 4.2 (release notes):
b. Subshells begun to execute command substitutions or run shell functions or
builtins in subshells do not reset trap strings until a new trap is
specified. This allows $(trap) to display the caller's traps and the
trap strings to persist until a new trap is set.
Normally, people would take this for granted. Consider this totally unsurprising Bash exchange:
bash$ trap
trap -- 'foo' EXIT
trap -- 'bar' SIGINT
bash$ trap | grep EXIT
trap -- 'foo' EXIT
Now look at the result in other shells like Dash, Ksh or Zsh:
dash$ trap
trap -- 'foo' EXIT
trap -- 'bar' INT
dash$ trap | grep EXIT
(no output)
This is perhaps more correct, but I doubt many people would expect it.
You appear to be reading an older version of the specification. In the most recent one,
When a subshell is entered, traps that are not being ignored shall be set to the default actions, except in the case of a command substitution containing only a single trap command, when the traps need not be altered.
Related
This is a very specific question, but I am interested to know what causes this behaviour
Observed
Run the following command and trigger "CTRl-C" signal after the fist wait echo.
bash -c "trap 'echo trapped' EXIT; until false &>/dev/null; do echo 'Waiting...'; sleep 3; done" | tee /tmp/test.txt
Notice the following error:
^Cbash: line 1: echo: write error: Broken pipe
Now run the following command and do the same:
bash -c "trap 'echo trapped' EXIT; until false &>/dev/null; do echo 'Waiting...'; sleep 3; done" | tee -i /tmp/test.txt
Notice how there is no error and the trap message is correctly shown.
Question
What would cause this behaviour and why does it seem that I need to use the -i/--ignore-interrupts option in order to correctly support trapping with tee?
Specs
GNU bash, version 5.1.12(1)-release
What causes CTRL-C trap echo to fail when using tee
It told you:
write error: Broken pipe
Because tee terminated, Bash can't write data to the pipe, which causes it to fail.
no --ignore-interrupts?
Because tee is not closed, Bash can write data to the pipe, so it does not exit.
What would cause this behaviour
Typing ctrl+c or sending SIGINT woudl cause tee to terminate, closing the pipe, and making Bash unable to send any more data to the pipe. As you do. (?)
why does it seem that I need to use the -i/--ignore-interrupts option in order to correctly support trapping with tee?
The statement in the question is false. The trap is executed, it just does not print the requested text, because the pipe is closed. the trap actually prints bash: line 1: echo: write error: Broken pipe error message.
I believe you meant to write the error to another file descriptor:
bash -xc "trap 'echo trapped >&2' ...
Also use set -x to debug your scripts, as above.
until null is super odd - just while true or while :. There is no command named null, you could do until false but that's like double negation.
I'm really trying to understand the difference in how ZSH and Bash are handling signal traps, but I'm having a very hard time grasping why ZSH is doing what it's doing.
In short, I'm not able to exit a script with exit in ZSH from within a trap if the execution point is within a function, unless it's also within a loop.
Here is an example of how exit in a trap action behaves in the global / file level scope.
#!/bin/zsh
trap 'echo "Trap SIGINT" ; exit 130' SIGINT
sleep 1
echo "1"
sleep 1
echo "2"
sleep 1
echo "3"
If I call the script, I can send an INT signal by pressing Cntrl+C at any time to echo "Trap SIGINT" and exit the script immediately.
If I hit Cntrl+C after I see the first 1, the output looks like this:
$ ./foobar
1
^CTrap SIGINT
But if I wrap the code in a function, then the trap doesn't want to stop script execution until the function finishes. Using exit 130 from within the trap action just continues the code from the execution point within the function.
Here is an example of how using trap behaves in the function level scope.
#!/bin/zsh
trap 'echo "Trap SIGINT" ; exit 130' SIGINT
foobar() {
sleep 1
echo "1"
sleep 1
echo "2"
sleep 1
echo "3"
}
foobar
echo "Finished"
If I call the script, the only thing that an INT signal does is end the sleep command early. The script will just keep on going from the same execution point after that.
If I hit Cntrl+C repeatedly the output looks like this.
$ ./foobar
^CTrap SIGINT
1
^CTrap SIGINT
2
^CTrap SIGINT
3
It doesn't echo the "Finished" at the end, so it is exiting when the function is finished, but I can't seem to exit before it's finished.
It doesn't make a difference if I set the trap in the global / file scope or from within the function.
If I change exit 130 to return 130, then it will jump out of that function early but continue script execution. This is expected behavior from what I could read in the ZSH documentation.
If I wrap the code inside of a for or while loop as shown in the code below, the code then has no problem breaking out of the loop.
#!/bin/zsh
trap 'echo "Trap SIGINT" ; exit 130' SIGINT
foobar() {
for i in 1; do
sleep 1
echo "1"
sleep 1
echo "2"
sleep 1
echo "3"
done
sleep 1
echo "Outside of loop"
}
foobar
echo "Finished"
Even if I have the loop in the global / file scope and calling foobar from within the loop, it still has no problem exiting within the trap action. I assume it's because using
The one thing that does work correctly is defining a TRAPINT function instead of using the trap built-in, and returning a non-exit code from that function. However exiting from the TRAPINT function works the same way it does with the trap built-in.
I've tried to find anything on why it acts like this but I couldn't find anything.
So what's actually happening here? Why is ZSH not letting me exit from the trap action when the execution point is inside a function?
One way to make this work as expected is setting the ERR_EXIT option.
From the documentation:
If a command has a non-zero exit status, execute the ZERR trap, if set, and exit. This is disabled while running initialization scripts.
There's also ERR_RETURN:
If a command has a non-zero exit status, return immediately from the enclosing function. The logic is similar to that for ERR_EXIT, except that an implicit return statement is executed instead of an exit. This will trigger an exit at the outermost level of a non-interactive script.
Both options have some caveats and notes; refer to the documentation.
Adding a setopt localoptions err_exit as the first line of the foobar function (You probably don't want to do this globally) in your script causes:
$ ./foobar
1
^CTrap SIGINT
$
Now, the interesting bit. In your demonstration script, if you change your exit value from 130 to some other number, and the echo lines to echo "1 - $?" etc., you get:
$ ./foobar
1 - 0
2 - 0
^CTrap SIGINT
3 - 130
The sleep is still exiting with 130, the normal value for a process killed by a SIGINT. What happened to your exit in the trap and its value? Not a clue (I'll update the answer if I figure it out) .
I'd just stick with the TRAPnal functions when writing zsh scripts that care about signals.
I have a fairly complicated bash script hierarchy that depends on functions for nearly everything. The main script is merely a shell that sources in appropriate functions (common ones like my output functions, etc. as well as specific ones for the task being executed) contained within their own files.
Within that framework, I have a particular function that needs to handle being interrupted and cleanup properly. I am handling it's error cleanup in a subfunction within that function
i.e.
function my_function {
function f_err_cleanup {
do error cleanup tasks here
} #/f_err_cleanup{}
echo do stuff
my_other_function -t "some var here"
my_other_other_function var1 var2 var3
echo do more stuff
} #/my_function{}
Adding a trap to the main script like this:
trap "declare -p FUNCNAME ; f_err_cleanup" INT QUIT TERM
fails due to it not knowing about the f_err_cleanup{} function - but the trap triggers.
^C./my_script.sh: line 1: declare: FUNCNAME: not found
./my_script.sh: line 1: f_err_cleanup: command not found
[ ERROR 13 ] Script "my_script.sh" failed.
Putting a trap inside my_function{} never triggers and cntl-c just drops out of the function with a 130 exit code (which the main script then handles generically.
trap "echo inside trap ; declare -p FUNCNAME" EXIT SIGHUP SIGQUIT SIGINT SIGTERM
The above snippet fails as follows when executed
^C[ ERROR 13 ] Script "my_script.sh" failed.
Here's a snip from the outer script:
. my_function
my_function "$#" 2>&1 | tee -a "${RUNTIME_LOG}"
declare -i RC=${PIPESTATUS[0]}
if [[ ${RC} -ne 0 ]]; then
f_err -f -c ${RC} -m "script \"${ME}\"failed\n"
else
f_out "success\n"
exit ${RC}
fi
f_err is a function to print an error message and exit with the provided code. How can I get the trap to work from within the function?
P.S. having the outer trap do a declare -p FUNCNAME returns that FUNCNAME is unknown as well, even though, as I understand it, it should show FUNCNAME[0]="main"
Putting a trap inside my_function{} never triggers
Yes it does. You just can't tell because you used echo to write to stdout, and stdout is a broken pipe because tee was killed by the sigint you're trying to handle.
Here's a MCVE:
#!/bin/bash
myfunction() (
trap 'mycleanup' INT
mycleanup() {
# Write to stderr so we can see it
echo "Cleaning up" >&2
}
echo "doing stuff"
sleep 42
echo "done with stuff"
)
myfunction | tee file
(The function's explicit subshell is not strictly necessary, but helps prevent accidentally botching the script's own traps)
Here's what happens when you run and interrupt it:
$ ./myscript
doing stuff
^CCleaning up
Posting as an answer because, while I have not resolved the conundrum of getting a trap to work when defined within a pipe, I have resolved my issue.
Because my pipe in this case is only to invoke tee to enable logging, I was able to change the piped line to
my_function "$#" &> >( tee -a "${RUNTIME_LOG}" )
Since there is no pipe involved anymore, the trap within the function will now trigger, leaving aside the questionable nature of bash error handling.
Here is the unexpected situation: in the following script, SIGALRM doesn't invoke the function alarm() at the expected time.
#!/bin/sh -x
alarm() {
echo "alarmed!!!"
}
trap alarm 14
OUTER=$(exec sh -c 'echo $PPID')
#for arg in `ls $0`; do
ls $0 | while read arg; do
INNER=$(exec sh -c 'echo $PPID')
# child A, the timer
sleep 1 && kill -s 14 $$ &
# child B, some other scripts
sleep 60 &
wait $!
done
Expectation:
After 1 second, the function alarm() should be called.
Actually:
alarm() is called until 60s, or when we hit Ctrl+C.
We know in the script, $$ actually indicates the OUTER process, so I suppose we should see the string printed to screen after 1 second. However, it is until child B exits do we see alarm() is called.
When we get the trap line commented, the whole program just terminates after 1 second. So... I suppose SIGALRM is at least received, but why doesn't it invoke actions?
And, as a side question, is the default behavior of SIGALRM to be termination? From here I am told that by default it is ignored, so why OUTER exits after receiving it?
From the bash man page:
If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will
not be executed until the command completes. When bash is waiting for an asynchronous command via the wait
builtin, the reception of a signal for which a trap has been set will cause the wait builtin to return immediately
with an exit status greater than 128, immediately after which the trap is executed.
Your original script is in the first scenario. The subshell (the while read loop) has called wait, but the top level script is just waiting for the subshell, so when it receives the signal, the trap is not executed until the subshell completes. If you send the signal to the subshell with kill -s 14 $INNER, you get the behavior you expect.
I'm a bit confused here. My goal here is to have the bash script exit with a non-zero exit code when any of the commands within the script fails. Using the -e flag, I assumed this would be the case, even when using subshells. Below is a simplified example:
#!/bin/bash -e
(false)
echo $?
echo "Line reached!"
Here is the output when ran:
[$]>Tests/Exec/continuous-integration.sh
1
Line reached!
Bash version: 3.2.25 on CentOS
It appears as though this is related to your version of bash. On machines that I have access to, bash version 3.1.17 and 3.2.39 exhibit this behaviour, bash 4.1.5 does not.
Although a bit ugly, a solution that works in both versions could be something like this:
#!/bin/bash -e
(false) || exit $?
echo $?
echo "Line reached!"
There are some notes in the bash source changelog which related to bugs with the set -e option.
I have seen this behavior in bash version 3.2.51 on both SuSE 11.3 and Mac OS prior to El Capitan. Bash 3.2.57 on El Capitan has the "correct" behavior, ie like bash 4.
However, the workaround proposed above, adding "|| exit $?" after the subshell's closing paren, defeats the intent of the -e flag no matter what version of bash. From man bash:
-e Exit immediately if a simple command (see SHELL GRAMMAR above) exits
with a non-zero status. The shell does not exit if the command that fails is
part of the command list immediately following a while or until keyword, part
of the test in an if statement, part of a && or || list, ...
A subshell followed by "|| exit $?" apparently counts as a command list; and the bash -e flag will not apply to ANY command inside the subshell. Try it:
$ set -e
$ ( echo before the error; false; echo after the error, status $?; ) || echo after the subshell, status $?
before the error
after the error, status 1
$
Because the subshell is followed by ||, that "echo after the error" is run, even with set -e. Not only that, the subshell exits 0, because that "echo" ran. So "|| exit $?" would not even run the "exit". Probably not what we wanted!
So far as I know, the following formula is compatible with bash versions whether they honor bash -e after subshell, or not. It even behaves correctly if the -e flag happens to be reset:
Add the following line immediately after the closing parenthesis of every subshell in the bash script:
case $?/$- in ( 0/* ) ;; ( */*e* ) exit $? ;; esac # honor bash -e flag when subshell returns
The -e option is for current shell, for sub-shell, functions, etc we use -E
form man bash
-E If set, any trap on ERR is inherited by shell functions, command substitutions, and commands executed in a subshell environment.
And for advanced users, a kind of strict mode we have which is:
set -Eeuo pipefail
-E explained above
-e exit immediately
-u exit for unbound variable found
-o set option
pipefail exit if we had failure on a pipe