Trap function by passing arguments?

Kounavi picture Kounavi · Feb 29, 2012 · Viewed 11.6k times · Source

I've been searching everywhere and I've come to believe that there is no way to do that other than having global variables but I believe the guru's in stackoverflow.com may be able to help me:

Is there any way in bash to trap a function by passing arguments to it?
For example, trap <function_name> <arg_1> <arg_2> SIGINT?

Answer

ruakh picture ruakh · Feb 29, 2012

trap lets you specify an arbitrary command (or sequence of commands), but you have to pass that command as a single argument. For example, this:

trap 'foo bar baz | bip && fred barney ; wilma' SIGINT

will run this:

foo bar baz | bip && fred barney ; wilma

whenever the shell receives SIGINT. In your case, it sounds like you want:

trap '<function> <arg_1> <arg_2>' SIGINT