Peng Yu
2018-12-07 20:00:51 UTC
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'
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
Regards,
Peng