Wednesday, March 2, 2011

Subshell vs Command Block

First, code inside a nested subshell obeys the above rules of subshell inheritance, except that it knows about variables defined in the surrounding shell; in contrast, think of blocks as code units that inherit everything from the outer shell. Second, variables and traps defined inside a command block are known to the shell code after the block, whereas those defined in a subshell are not.
1. Subshell

If you run this code, you will see the message $hatter  is mad every 60 seconds, and if you hit CTRL-C, you will see the message,  You hit CTRL-C!. You will need to hit CTRL-Z to  stop it (don't forget to kill it with kill %+).
2.  Command Block


If you run this, you will see the message $hatter is; the outer shell doesn't know about the subshell's definition of hatter and therefore thinks it's null. Furthermore, the outer shell doesn't know about the subshell's trap of the INT signal, so if you hit CTRL-C, the script will terminate.




No comments: