help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] Are all traps global to a script?


From: Peng Yu
Subject: [Help-bash] Are all traps global to a script?
Date: Fri, 7 Dec 2018 14:00:51 -0600

Hi,

I see that traps seem to be all global. For example, if I set it in a
function, it will have an effect outside a function.

What is the best way to make effectively different traps for different
function? Does bash have a native support for this?

I can think of setting some special variables in each function so that
the trap can check this variable to know where the program is at, and
acts differently. Is there any better solution? Thanks.

$ ./main.sh
in f
^CHello World!
after f
$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

trap "echo 'Hello World1!'" INT
function f {
    trap "echo 'Hello World!'" INT
    echo 'in f'
}
f
sleep 10
echo 'after f'


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]