When you enter a command on the command line and press enter, the command interpreter performs several actions before executing the command. These are done in a particular order.
History expansion - the line is analyzed for history references or the bang, !. Bang references are expanded and resolved using the current history list and the history recall modifiers, such as :2:h.
History list updated - the new command line is stored in the history buffer. No other command line expansion or parsing is done at this point.
Alias replacement - next any alias references are replaced with their definition.
Command line parsing - The shell then parses the line into its various commands using recognized delimiter, end of line, semi-colon, pipe, &&, ||, and &.
This is also the point at which redirection is set up.
However, the following steps are applied to each command if/as it is run.
In the following example : ls > f1; wc f1 || ls -l > f1; head f1 f1 will have the output of the original ls. The redirection in the 3rd command in the sequence does not occur if wc runs successfully.Brace expansion - expand any list inside of file-name wild-card braces. This does NOT apply to the use of braces in variable expansion.
Tilde expansion
Parameter/variable expansion - resolves and expands positional parameters, such as the $1-9, and $*. Also performs defined variable substitution and variable brace, {}, resolution. See parameter expansion section in the bash man page.
The IFS definition can have an effect on how $* and $@ are expanded.
Command substitution - run the command specified and paste its output in the command buffer. You may use either ` ` or $( ) to specify command substitution.
> cmd="ls /" > echo `$cmd` boot cdrom dev etc export home initrd initrd.img lib lib32 lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var vmlinuz` |
Arithmetic expansion - bash provides its own arithmetic evaluation mechanism in place of running the expr command. To use arithmetic expansion, enclose the formula in $(( )).
echo $(( 4 + 3 ))
If you work mostly in the bash shell, this is more efficient than
invoking the
expr command each time. On the other hand, if you work in
several shells, using expr will provide a more consistent programming
practice.
Word splitting - at this point, separation of unquoted strings is recognized. Word splitting works with the IFS (internal field separator) variable to determine how a string is to be parsed. The IFS affects how the read command handles input.
#( Try the following ) #( Start a new copy of the bash shell. If you have trouble resetting the IFS variable, you can terminate the child shell. )
bash read v1 v2 #(When prompted, enter the following) one:two three echo $v1 echo $v2 #(Display the current contents of the IFS variable ) echo "$IFS" | cat -vet #( Reset IFS to use : as a delimiter ) IFS=: read v1 v2 #(When prompted, enter the following) one:two three echo $v1 echo $v2 #(You should now see the input split at the : rather than the space ) #(To reset IFS, use IFS="[space][ctrl]v[tab][enter]" ) #(To test IFS, Display its current contents ) echo "$IFS" | cat -vet #( Quit the child shell ) exit |
Filename wild-cards - expand the filename wild-cards.
Quote Removal - Any unquoted occurrences of \, ', and " that did not result from a previous expansion are now removed.
When you echo a quoted string, you get the contents of the quotes but not the quotes themselves.