[Top][All Lists]

[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


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.

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

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


reply via email to

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