Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
Showing results for 
Search instead for 
Did you mean: 

exporting 'trap' to other shell

Chandrahasa s
Valued Contributor

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)
James R. Ferguson
Acclaimed Contributor

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.


muruganantham raju
Valued Contributor

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.

echo "hello"
export MYCMD="echo Today is `date '+%d %m %y'`"

# cat
echo "`$MYCMD`"