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:
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}
Filename expansion (also know as globbing)
|
1 2 |
*.sh -> expands to all file names that have an extension of .sh [st]* -> expands to all file names that start with either an 's' or a 't' |
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*’
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