Bash has a useful feature to make an alias to a command you often use. It’s essentially a shortcut or abbreviation for the longer command sequence. For instance, if you find yourself using often the command
ls -alF you can create an alias for it.
Just add it to the file
~/.bashrc. Open it with your favorite editor and add the following lines:
alias ll='ls -alF'
.bashrc (or start a new terminal session):
Now you can use the command
ll to display directory listings.
An alias I often use is for WP-CLI. You’re not allowed to run
wp as root. It is therefore better to run it as the user that is also used to run the website (for instance
www-data). Add this line to
/root/.bashrc to fix this issue:
alias wp="sudo -u www-data -- wp"
Look up aliases
If you need to know what specific command is executed when you run an alias, you can use the
$ type ls ls is aliased to `ls --color=auto' $ type wp wp is aliased to `sudo -u www-data -- wp'
When you do not know exactly which commands are aliased, you can use the
compgen command to list all available aliases:
$ compgen -a egrep fgrep grep l la ll ls
“What about zsh?” you might ask. The answer is short, it is exactly the same except for one thing. You just add the aliases to
.zshrc instead of