Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

exporting 'trap' to other shell

Valued Contributor 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)
Chandra
2 REPLIES
Acclaimed Contributor 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:

#!/usr/bin/sh
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.

Regards!

...JRF...
Valued Contributor 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.

#cat test.sh
#!/usr/bin/sh
echo "hello"
export MYCMD="echo Today is `date '+%d %m %y'`"
/tmp/child.sh

# cat child.sh
#!/usr/bin/sh
echo "`$MYCMD`"

HTH
Muru