Skip to content

Command Line Expansion Commands and Braced Sets in Bash

26 October 2022

Learn about Command Line Expansion Commands and Braced Sets in Bash, and how to use them to generate arbitrary strings and create multiple modified command-line arguments out of a single argument.

Commands and Braced Sets

  • Command Expansion: $( ) or ``
    • Prints output of one command as an argument to another

      $ echo “This system’s name is $(hostname)”
      This system’s name is server10.example.com
  • Brace Expansion: { }
    • Shorthand for printing repetitive strings

      $ echo file {1,3,5}
      file1 file3 file5
      $ rm -f file{1,3,5}

The bash shell has some special features relating to expansion which significantly improve the power of working at the command line.

Curly braces are useful for generating patterned strings. For example, without the curly braces, the mkdir
command below would take almost two hundred keystrokes to execute.

[amar@amar-pc ~]$ mkdir -p
work/{inbox,outbox,pending}/{normal,urgent,important}

Use of the backquotes is called command substitution. In command substitution, the command in backquotes is executed and the output of the command is placed on the command line as if the user had typed it in. An alternative syntax for the backquotes is to place the command in parentheses preceded by a dollar sign $( ).