When we type a command with arguments/inputs and press the enter key, the shell
does several things to the arguments/input text before it actually carries out
the command. This action is called expansion. With expansion, the arguments
expands into something else before the shell acts on it with the command. Let's
see an example with echo command which prints out the text arguments given on
standard output.
input
1
echohelloworld!
output
1
hello world!
Let's use echo with *:
input
1
echo*
output
1
hello.c hello.qsub hello.sb README
Instead of printing *, it prints out all file names in the directory because
shell expands * into something else before the echo command acts on the
argument (in this case *).
input
1
ls
output
1
hello.c hello.qsub hello.sb README
The * character is called a "wildcard" is replaced by everything in the
current directory. We can also modify it so that it's only replaced by certain
things. For example, h* is replaced everything that starts with h in the
current directory.
input
1
echoh*
output
1
hello.c hello.qsub hello.sb
~ is another special character with a special meaning. It expands
into the name of the home directory of the user:
input
1
echo~
output
1
/mnt/home/temp_user_01
The shell will also expand arithmetic. Arithmetic expansion uses the form $((expression)). Look at the example.
input
1
echo$((1+1))
output
1
2
Please keep in mind that arithmetic expansions allows only integers.
Arithmetic expression can be nested, and spaces are allowed.
input
1
echo$((7*(2+2)))
output
1
28
input
1
echo$((7*(2+2)))
output
1
28
Brace expansion is useful when you write a shell script or batch script. The
brace expression can contain a comma separated list of characters, strings, or
integers that will be expanded into multiple expressions. Here are a few
examples.
Expansion also allows us to use the output of a command in different places.
The syntax is the same as arithmetic expansion, except the command is placed
inside $(...)
Next, let's learn how to control expansion. Consider the following two examples
where automatic expansion doesn't do what we want.
input
1
echoThisisatest
output
1
This is a test
input
1
echoThetotalis$100.00
output
1
The total is 00.00
In the first example, the shell removes extra space from the echo command's
argument. In the second example, $1 is interpreted as the first input
parameter which is not defined here, and therefore, it is replaced as empty
string. With quoting, we can suppress unwanted expansions.
First, let's learn about double quotes. If we place text inside double quotes,
parameters are not split by white space and all special characters lose their
special meaning, and are treated as ordinary characters. However $, \
(backslash), and ` (back quote) are exceptions.
input
1
echo"This is a test"
output
1
This is a test
input
1
echo"The total is $100.00"
output
1
The total is 00.00
Here is another example where the shell splitting the parameters by white space
would cause a problem. Suppose that our working directory has the file two
words.txt in it, where the filename has a space.
input
1
lstwowords.txt
output
12
ls: cannot access two: No such file or directoryls: cannot access words.txt: No such file or directory
The shell splits the two words and looks for them each separately with ls.
Quoting fixes this.
input
1
ls"two words.txt"
output
1
two words.txt
If you want to suppress all expansions, you need to use single quotes.
input
1
echo'This is a test'
output
1
This is a test
input
1
echo'The total is $100.00'
output
1
The total is $100.00
The next three examples show how quoting gives different results.
input
1
echotext~/*.txt{1..5}$(echofoo)$((2+2))$(date)
output
12
text /mnt/home/user_name/hostfile.txt /mnt/home/user_name/powertools.txt 1 2 3 4 5 foo 4 Tue Jan 19 15:10:00 EST 2021
A backslash is useful when we want to quote a single character. A
backslash is called the escape character. Next example shows how quoting
and an escape character work.
input
1
echoThebalanceof$(date)is$100
output
1
The balance of Tue Jan 19 15:15:36 EST 2021 is 00
input
1
echo"The balance of $(date) is $100"
output
1
The balance of Tue Jan 19 15:15:48 EST 2021 is 00
input
1
echo'The balance of $(date) is $100'
output
1
The balance of $(date) is $100
input
1
echo"The balance of $(date) is \$100"
output
1
The balance of Tue Jan 19 15:16:09 EST 2021 is $100
input
1
echo'The balance of $(date) is \$100'
output
1
The balance of $(date) is \$100
The table show the most frequently used escape characters.