Help more people learn by sharing this post!

Bash expansions

In this post you will learn about bash expansions, you probably already used them if you have done something like “ls *.txt”. Basically the shell does some processing on the command line before actually executing it, which enables us to do a number of things.

Here is a small cheatsheet of the most useful expansions:

Brace expansion

List: {1,2,3}
brace-expansion

This one can be useful to rename a file, for example you can rename access.log to access.log-old using this: mv access.log{,-old}

Range: {20..30}
Bash expansions

Filename expansion (also know as globbing)

filename-expansion

File expansion is very useful when you need to handle multiple files at the same time. If you need the full path as script input use find instead.

Tip: if you want to use a especial character without it being expanded you can enclose it in quotes, for example: echo ‘test*’

Variable expansion

echo $SHELL -> /usr/bin/zsh
echo $HOME -> /root

Tip: You can use the ‘env’ command to list all your environment variables. You may wanna pipe the output through less.

You can learn more here: http://www.gnu.org/software/bash/manual/html_node/Shell-Expansions.html