Skip to main content

1. Shell expansions

  1. Wildcard expansions:

    echo this is a test

    echo displays all the arguments that are passed to it.

    cd /usr
    ls
    echo *

    The "*" character means match any characters in a filename. The shell expands the "*" before executing the command echo.

    echo lib*
    echo *bin
    echo lib*32
    echo */share
    echo /*/*/bin

    A question mark "?" matches any single character:

    echo lib??
    echo lib???

    Character sets are enclosed in square brackets:

    echo lib[123456789]?
    echo lib[xyz][123456789]?

    Character ranges:

    echo lib[1-9][1-9]
    echo lib[a-z][1-9][1-9]
    echo lib[1-9][!2]

    Character classes:

    echo lib[[:digit:]][[:digit:]]
    echo lib[[:alpha:]][[:digit:]][[:digit:]]
    echo lib[[:alnum:]][[:alnum:]][[:alnum:]]
    echo lib[![:digit:]]*
    echo lib[36[:lower:]]*
    echo /etc/[[:upper:]]*
  2. The tilde character ("~") expands to the home directory:

    echo ~
    echo ~root
  3. Arithmetic expansion:

    echo $((2 + 2))
    echo $(($((5**2)) * 3))
    echo $(((5**2) * 3))
    echo Five divided by two equals $((5/2))
    echo with $((5%2)) left over
  4. Brace expansion:

    echo Front-{A,B,C}-Back
    echo Number_{1..5}
    echo {01..15}
    echo {001..15}
    echo {Z..A}
    echo a{A{1,2},B{3,4}}b
    cd
    mkdir Photos
    cd Photos
    mkdir {2017..2019}-{01..12}
    ls
  5. Variable expansion:

    echo $USER
    printenv | less
    echo $SUER

    When the variable does not exist, it is expanded to the empty string.

  6. Command substitution:

    echo $(ls)
    ls -l $(which cp)
    echo "$(cal)"
Loading asciinema cast...