Double Command−Line Scanning

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

Shell variables are often used on the shell command−lines, as a part of UNIX or shell commands. Unfortunately, sometimes they can easily be misinterpreted. Simply, under certain conditions, shell variables could be understood literally: the variable VARA from the previous example can be understood as VARA instead of its value VariableA. Just think about versatile and powerful UNIX commands better to say UNIX utilities like, awk, sed, or other commands that have their own syntax somehow different from the shell syntax. This makes a great difference and could make the use of shell variables very restricted. The shell response to this situation is the command: eval. This command allows so−called double command−line scanning, where the shell variables are first processed, developed, and then replaced for the second command−line processing. For better understanding of this command, let us see how the shell command interpreter processes a command line at all. This is presented in Figure 3.2 and explained in the following text. 83 Figure 3.2: Shell processing of the command line. The command line is tokenized, i.e., split into its constituents: word, keywords, IO redirectors, and semicolons, according to the separating metacharacters: space, tab, new line,, , , , \, , and . 1. The first token is tested if it is a single−line unquoted keyword a keyword without quotes or continuation character. Shell statements if, while, until … and functions are treated as opening keywords, set up internally; the processing continues with the next token. 2. The command is tested against the list of command aliases; eventual aliases are expanded and reprocessed. 3. The substitution of an eventual users home directory. 4. The variable substitution for any expression with leading . This is also the second 5. 84 6. The evaluation of the arithmetic expressions of the form expression. Remember that the double−quoted expressions are processed differently from others after this step. 7. The eventual expanded text as a result of the previous step processing is now tokenized according to the shell environment internal field separators IFS. 8. The wildcard expansion of , ? and [] pairs, and processing of regular expression operators. 9. The search for the command in all predefined command directories according to the shell PATH or path variable. This is also the second, and the only, step in processing single−quoted command−line tokens. 10. At this point everything is ready for the command−line execution. However, if the shell command eval was specified, another round of the command processing will be performed. This is known as double command−line scanning. The format of the command is: eval args where args includes the actual command itself and command arguments. For better understanding of this command, see the following example. The users shell is bash, but it does not have any specific impact on the example could be any other shell. bash VAR1=VAR2 Define variable VAR1 bash VAR2=Example Define variable VAR2 bash echo VAR1 Check the values of variables VAR2 bash echo VAR2 Example bash eval echo VAR1 Check the values of variables upon double scanning Example bash eval echo VAR2 Example

3.5.2.4 Here Document