DEV Community

Chibuzor Ojukwu
Chibuzor Ojukwu

Posted on

The Concept of Expansion in the Linux Command Line

Linux Command Line
In this short article, we will be exploring an important concept in linux shell called expansion.

When you type a command and press the Enter key, bash performs several processes upon the argument text before it executes the command. This process is called “expansion”.

Let’s see the different types of expansion within the shell.

1) Pathname expansion: The shell first expands the path (current directory) to get all the files that end with .js (see image below) after which the ls command is performed on the result.

pathname expansion

2) Tilde expansion: From the example below, the shell expands the tilde (~) sign to get the home directory of the user before executing the echo command on its result.

tilde expansion

3) Arithmetic expansion: From the screenshot below, the shell performs the computation (expansion) before the echo command is executed on its result.

arithmetic expansion

4) Brace expansion: This is the most fascinating expansion. It simplifies complex tasks with just a single command. After expansion, dir-{1..5} becomes dir-1 dir-2 dir-3 dir-4 dir-5, before the mkdir is performed on them, creating 5 directories.

brace expansion

5) Parameter expansion: This is more useful in shell scripts than directly on the command line. It is also known as variables. From the image below, the shell expands $USER to get its value and passes it to the echo command. Use the printenv command to see all variables.

parameter expansion

6) Command substitution: This allows you to use the output of a regular shell command as an expansion. See example below

command substitution expansion

Lastly for this session, we will discuss a concept called “quoting” used to control expansion performed in shell. It is used to suppress unwanted expansions. There are 2 types of quoting, double and single quotes.

Double quotes: This will suppress any kind of special characters usually meant for expansion and treat them as ordinary characters except for $ (dollar), \ (backslash), and ` (back tick) signs.

double quoting

Single quotes: With this, you can suppress all expansions.

single quoting

I hope you learnt about a few of the superpowers that the Linux Shell embodies.

Thanks for your time. Your feedback will be much appreciated.

Top comments (0)