Expansion and substitution
takes place in the following sequence:
1. Brace
expansion
2. Tilde
expansion
3. Parameter
expansion
4. Command
expansion
5. Arithmetic
substitution
6. Word
splitting
7. Pathname
substitution
Brace
expansion:
In
brace expansion, the shell looks for braces ({}) – also called curly brackets –
in the token. If braces are present, it expands their contents.
For Eg:
The
token b{all, ook} expands into
ball book
Tilde
expansion:
The
shell looks for tildes (~) in the token. If a tilde is present, it replaces the
tilde with the location of the current user’s home directory.
For Eg:
Depending on the system
configuration, the token ~che/file2 might expand into
/export/home/che/file2.
Parameter
substitution:
In parameter substitution,
the shell checks whether the token is variable name preceded by a dollar sign
($). If it is, the shell replaces the token with the current value of
corresponding variable.
For Eg:
If the value of the SHELL
parameter is /bin/bash, the token $SHELL is replaced with /bin/bash.
Command substitution:
In command substitution,
the shell checks whether the token is a command enclosed in brackets and
preceded by a dollar sign ($). If it is, the shell processes the command and
replaces the token with the command’s output.
For Eg:
The token $(type username)
might be replaced with che.
Arithmetic
substitution:
In arithmetic
substitution, the shell checks whether the token is an arithmetic expression
enclosed in double brackets and preceded by a dollar sign. If it is, the shell evaluates the expression
and replaces the token with the result.
For Eg:
The replaces the token
$(72/9) with 8.
Word splitting:
In word splitting, the
shell examines those parts of the command line that have resulted from previous
stages of expansion and substitution. If
any of these contain spaces or special characters, it splits them into tokens
for processing.
Pathname
substitution:
In pathname substitution,
the shells look for wildcard characters in the token. If it finds asterisks
(*), question marks (?), or double slashes(//), it searches the current
directory for filenames that match these wildcards and substitutes them for the
token.
For Eg:
Depending on the files in
the current directory, the token f*.txt might expand into fat.txt, fund.txt, fight.txt.