Shell Variables UNIX Shell Scripts

bash cat tmpmyscript1.bash binbash Define variables export TEXT1=This is a bash script myscript1.bash export TEXT2=Running the script myscript1.bash Run the command echo TEXT1 echo TEXT2 bash tmpmyscript1.bash This is a bash script myscript1.bash Running the script myscript1.bash csh tmpmyscript1.bash This is a bash script myscript1.bash Running the script myscript1.bash In all the examples, the current shell spawns itself or another shell, making a parent–child relationship between two shells current users shell and the invoked shell script. However, a shell script can also be executed directly in the users shell environment. For this purpose the shell script must be sourced. A special shell command is used to source the script. source myscript.sh for csh and csh−like shells. .myscript.sh for ksh, bash, and Bourne shells To source a shell script means to skip the forking of the users shell and to execute the script directly in the users shell environment.

3.5.2.2 Shell Variables

We can define and redefine shell environment within the shell script. By invoking a new shell script, the current shell environment is transferred and the new initial shell environment created. Remember that this is a unidirectional transfer, from parent toward child shell child inherits the parents environment; the reverse is never possible. Regarding shell variables, only global, i.e., exported, variables could be inherited; local variables remain always within the current shell environment, and they disappear once the shell is terminated. This sometimes sounds very confusing for the novices in UNIX administration. In this light we can better understand the need and purpose of the shell command: source. If we want to define a shell environment within a single script let us call it environment definition script, and then share these definitions among many other shell scripts, we must source the environment definition script. Otherwise, all definitions will last as long as the execution of the environment definition script. The following example illustrates that situation. The users shell is Bourne shell. Variables VARA and VARB are not defined. sh echo VARA To check if VARA is defined 82 The script tmpmyscript2.sh defines the global variables VARA and VARB: sh cat tmpmyscript2.sh Variable definitions VARA=VariableA VARB=VariableB Export VARA VARB Upon the script execution, variables VARA and VARB are still undefined in the users shell environment. There is no way to export variables toward the parent shell environment. sh tmpimyscript2.sh Execute the script sh echo VARA To check if VARA is defined sh echo VARB To check if VARB is defined Upon the sourcing of the script variables, VARA and VARB remain defined within the users shell environment. sh . tmpmyscript2.sh Source the script sh echo VARA To check if VARA is defined VariableA sh echo VARB To check if VARB is defined VariableB The previous discussion is instrumental in understanding the users log−in process and the initial definition of the users shell environment, which is discussed in Chapter 7.

3.5.2.3 Double Command−Line Scanning