exporting 'trap' to other shell (76 Views)
Reply
Valued Contributor
Chandrahasa s
Posts: 219
Registered: ‎11-09-2009
Message 1 of 3 (76 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)
Chandra
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 3 (76 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:

#!/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
muruganantham raju
Posts: 149
Registered: ‎06-29-2010
Message 3 of 3 (76 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
#!/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
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.