Expansions in Linux
published on Wed Mar 25 2020When we run a command on Linux, there are quite a few substitutions that are done by bash before execution. The process that makes this happen is called expansion.

Pathname Expansion
The mechanism by which wildcards work is called pathname expansion. For examples on how to use, check out this post
Tilde Expansion
Tilde (~
) expansion, by default, points to the home directory of the current user.
Arithmetic Expansion
Arithmetic expansion supports basic arithmetic such as addition (+
), subtraction (-
), multiplication (*
), division (/
), modulo (%
) and exponentiation (**
). The syntax of arithmetic expansion looks like this:
$((expression))
echo $(($((5**2)) * 3))
Brace Expansion
This is mostly useful to make a list of files or directories. It works like this:
echo Front-{A,B,C}-Back
Front-A-Back Front-B-Back Front-C-Back
echo Number_{1..5}
Number_1 Number_2 Number_3 Number_4 Number_5
Parameter Expansion
Variables stored by the system are expanded by bash to get their values. For example, USER
is a variable containing our username. We can expand it like this:
echo $USER
saikat
Command Substitution
This allows us to use the output of a command as an expansion. For example, we could
ls - $(which cp)
-rwxr-xr-x 1 root root 146880 Feb 28 2019 /bin/cp
Escaping expansion
In case we wish to escape bash expansion, we can put the string section that is likely to be expanded inside double ("
) quotes. For example:
Without quotes
echo $(cal)
March 2020 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
With quotes
echo "$(cal)"
March 2020
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31