Bash – set -T in bash – what does it do

bashshellunix

What does set -T mean in bash? What does it do? I believe it is related to traps in Unix but I am not sure.

I found this:

Many of such constructs become more simple if traps would be called
immedeately, while is foreground child is still running. You would
just install a trap handler that does "something" about the problem
and it would be called everytime you hit SIGINT (or SIGQUIT). Just as
signals handler in C programs are called immedeately. Maybe I am too
much of a C programmer, but I find the delayed sh behaviour very
non-intuitive.

#! /bin/sh

onsig()
{     trap SIGINT     kill -SIGINT $$
}
set -T            # set async execution of traps on FreeBSD
trap onsig SIGINT
./some-blocking-program
set +T            # set traps execution behaviour back to normal

This makes the trap handler a bit more complicated, but it allows you
to write the main part of your shell script as usualy, without keeping
in mind that a program may block and taking the appropriate action
about it.

Best Answer

From help set:

  -T  If set, the DEBUG trap is inherited by shell functions.

So if you use trap to invoke a function on DEBUG (that is, almost before every command in a shell script) and then invoke another shell script, the trapping will occur in that script as well. Without this option, the trap will not exist in the subshell and the script invoked in it will run untrapped.