exporting 'trap' to other shell (86 Views)
Valued Contributor
Posts: 219
Registered: ‎11-09-2009
Message 1 of 3 (86 Views)

exporting 'trap' to other shell

Hi Gurus,

I want to know is there any way to export following line to new shell also.
trap 'date "+# %c" | read -s' debug

when i do sh and enter to new shell this trap stop working (there need to run again same)
Acclaimed Contributor
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 3 (86 Views)

Re: exporting 'trap' to other shell

Hi Chandra:

You can export variables but not code. In this case, you could place the line in question into a file that you source (or "include") when spawning a new shell. Thus, any shell could do:

echo "I am born..."
. /usr/local/bin/myshtraps

Where '/usr/local/bin/myshtraps' contains the line of code you described. Note that shell's sourcing (reading or including) the material are called with a dot ('.') followed by whitespace and the file name. This adds the code and/or variables to the current environment for its use rather than spawning a new subshell.


Valued Contributor
Posts: 149
Registered: ‎06-29-2010
Message 3 of 3 (86 Views)

Re: exporting 'trap' to other shell

hi Chandra,

If it is a single line of command, then you can assing the command to a variable and export the variable.

For E.g.

#cat test.sh
echo "hello"
export MYCMD="echo Today is `date '+%d %m %y'`"

# cat child.sh
echo "`$MYCMD`"

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.