- Shell Evaluation Order
- Short Circuit Shell Evaluation Order
- Eval Command Description
- Eval Command Short Circuit
- Eval Command Examples
- Importance of Shell Evaluation Order
- Bread Crumb Navigation
-
Token Recognition
- Words in the command line are split into tokens separated by metacharacters
- White Space
;
(Semicolon),|
,<
,>
,\
, - Substitution can occur here like
$HOME/myfile
-
Shell then looks if there is an opening keyword if keyword then the shell knows it is a compound command and that it needs to collect all of the actual command before executing else if middle or final keyword //
elif
orfi
then keep going and collect all of the command or finish everything and go to step 3 else a syntax error has occurred and shell exits or in a interactive session just go back to shell prompt -
Is the word an alias ? if token an
alias
then expand alias and go to step 1alias ll='ls -lh'
the shell would expand ll in this case tols -lh
else continue on to step 4 -
Tilde Substitution occurs in following situations:
- On the start of a word
- After
:
,:-
in variable assignment as we mentioned previously - In expressions like
${somevar operation aword}
-
Parameter or aka variable expansion
NAME=james ; echo $NAME
- Here the
NAME
variable is expanded to james
-
Command substitution
- Old style with backticks ``
- New style with
$()
-
Arithmetic expansion
$(())
-
Split results of Step 5, 6, 7 onto characters in the Internal Field Separator aka $IFS variable
-
For many command line interpreters ("shell") of Unix operating systems, the internal field separator (abbreviated IFS) refers to a variable which defines the character or characters used to separate a pattern into tokens for some operations.
-
IFS typically includes the space, tab, and the newline.
-
This step is distinguished by steps you can control as shell programmer
-
if there is whitespace then each run of whitespace will separate fields
-
else each non whitespace will delimit a field
-
-
Filename generation / Wilcard Expansion
-
In a series of tokens then look at first word
-
Keywords will be found but really any keywords have already been found
-
Look at special builtins:
break, continue
-
Functions
-
Regular builtins
cd, echo
-
Setup Input and Output I/0 redirections that were saved from step 1 and finally to run the shell program
-
Anything in single quotes ' ' is taken literally and shell program execution goes straight to step 11
-
Anything in double quotes " " goes from token recognition Step 1 ==> Step 5 (Variable Expansion)
-
If you use the eval command then shell program execution will go from Step 11 ==> Step 1
eval: eval [arg ...]
-
Execute arguments as a shell command.
-
Combine ARGs into a single string, use the result as input to the shell, and execute the resulting commands.
-
Exit Status:
- Returns exit status of command or success if command is null.
Eval will rerun each of the steps given previously
The main purpose of eval is to build up a command in a string and then to run it
mycommand="echo $?"
if [ "$SOMEVAR" -eq "$ANOTHERVAR" ]; then
mycommand="$mycommand | less"
fi
eval "$mycommand"
When we run eval
then command will be run albeit this is a senseless case of eval
testcommand="man test"
command="$testcommand | more"
echo $command
This will just echo the command and treat values literally but not execute it
eval $command
This will actually execute the $command and run the man page through the more utility
cd scripts/eval-order
mkdir tmp
cd tmp
cat > tempFile
Some temporary file
Just a test here
Press Control D to redirect stdin into tempFile
read -p 'Enter something here: ' something
Here we are getting user supplied input
I put in rm *
as my input
set -x
Set execution tracing so you see what is happening
eval $something
Notice here that tempFile has been deleted
ls
You need to understand shell evalution order in order to write correctly working shell scripts
Shell Interactions can be tricky to see when they occur:
- Substitutions can occur during token parsing
- Nested shell constructs happen during command substitution
- Substitutions can occur in double quoted contexts
Using eval* can provide a mechanism to short circuit shell evalution order
*An important caveat is that using eval can be a possible security vulnerability as you see in the example shown above
Don't ever run eval with user supplied input because a malicious user could be attempting a malicious attack
Previous | Next |
---|---|
← History Substitution | Subshells → |