Saturday, October 24, 2009

Exported Variables and Subshell Execution








 

 










Exported Variables and Subshell Execution



Commands other than the shell's built-in commands are normally executed in a "new" shell, called a subshell. Subshells cannot change the values of variables in the parent shell, and they can only access variables from the parent shell that were exported to them�either implicitly or explicitly�by the parent. If the subshell changes the value of one of these variables and wants to have its own subshells know about it, it must explicitly export the variable before executing the subshell.



When the subshell finishes execution, any variables that it may have set are inaccessible by the parent.



The (...) Construct



If one or more commands are placed inside parentheses, those commands will be executed in a subshell.



The { ...; } Construct



If one or more commands are placed inside curly braces, those commands will be executed by the current shell.



With this construct and the (...) construct, I/O can be redirected and piped into and out of the set of enclosed commands, and the set can be sent to the background for execution by placing an & at the end. For example,





(prog1; prog2; prog3) 2>errors &



submits the three listed programs to the background for execution, with standard error from all three programs redirected to the file errors.



More on Shell Variables



A shell variable can be placed into the environment of a command by preceding the command name with the assignment to the parameter on the command line, as in





PHONEBOOK=$HOME/misc/phone rolo



Here the variable PHONEBOOK will be assigned the indicated value and then placed in rolo's environment. The environment of the current shell remains unchanged, as if





(PHONEBOOK=$HOME/misc/phone; export PHONE BOOK; rolo)



had been executed instead.












     

     


    No comments:

    Post a Comment