Expansion
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.
1 2 |
|
Let's use echo
with *
:
1 2 |
|
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 *
).
1 2 |
|
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.
1 2 |
|
~
is another special character with a special meaning. It expands
into the name of the home directory of the user:
1 2 |
|
The shell will also expand arithmetic. Arithmetic expansion uses the form $((expression))
. Look at the example.
1 2 |
|
Please keep in mind that arithmetic expansions allows only integers. Arithmetic expression can be nested, and spaces are allowed.
1 2 3 4 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 |
|
The next example creates multiple directories with a brace expansion.
1 2 3 4 5 6 |
|
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 $(...)
1 2 |
|
Next, let's learn how to control expansion. Consider the following two examples where automatic expansion doesn't do what we want.
1 2 3 4 5 |
|
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.
1 2 3 4 5 |
|
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.
1 2 3 |
|
The shell splits the two words and looks for them each separately with ls
.
Quoting fixes this.
1 2 |
|
If you want to suppress all expansions, you need to use single quotes.
1 2 3 4 5 |
|
The next three examples show how quoting gives different results.
1 2 3 4 5 6 7 |
|
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The table show the most frequently used escape characters.
Escape Character | Name | usage |
---|---|---|
\n | newline | Adding blank lines to text |
\t | tab | Inserting horizontal tabs to text |
\a | alert | Making the user terminal beep |
\\ | backslash | Inserting a backslash |