5 Types of Expansions on Linux

A small feature can make your life much easier

Yang Zhou
TechToFreedom

--

Art of squares under yellow background
Image from Wallhaven

Bash has a special mechanism called expansion, which gives us much convenience. So we can use some notations to write a short command that will be expanded into something complex before being executed.

For example, with the help of brace expansion, it’s easy to generate 12 files each for one month through a short command:

touch report_2022_{01..12}

This article will introduce 5 significant types of expansions in bash on Linux. If you can master them, you will be able to write neater and cleaner bash commands or scripts like a guru. 😎

1. Pathname Expansion

The pathname expansion is a commonly used trick for DevOps developers to handle directories and files. Essentially, it leverages the power of the wildcards.

For example, if we would like to list the files whose names start with a capital letter, we can write the command as follows:

ls [[:upper:]]*

There are 3 points of the above command:

  • The * is a wildcard which can match any characters.
  • The [characters] syntax is to match any character inside the [].
  • The [:upper:] is a…

--

--