If you live in the Linux Shell/Terminal, aliases are a massive timesaver. Here’s how to create your own Bash aliases, with examples.
What is an Alias in Bash/Linux Shell?
An alias is a shortcut to a longer command. It’s similar to a keyboard shortcut – like the CTRL + C key combination is a shortcut to the copy command in many graphical operating systems (saving the time in dragging your mouse across the screen and clicking multiple menus to reach the command), aliases are shortcuts to longer terminal commands (saving time typing out the full command).
Bash provides aliasing functionality built-in, as do many other Linux Shells, including zsh, which shares similar syntax.
The examples in this article should work with both Bash and Zsh.
Creating an alias
The alias command is used to create an alias.
alias and unalias Command Syntax
Here’s the syntax for the alias command:
alias OPTIONS SHORTCUT=COMMAND
And here’s the syntax for the unalias command:
unalias OPTIONS SHORTCUT
Note that:
- OPTIONS is a list of optional flags from the below table
- SHORTCUT is the shortcut you wish to be able to type to execute COMMAND
- it can only consist of alphanumeric characters, dashes, and underscores
- COMMAND should be the command you wish to be executed when SHORTCUT is entered
- The = character should separate SHORTCUT and COMMAND with no spaces between
- If COMMAND contains spaces, you will need to contain it within quotes
- An alias can only be used as the first word in a command
alias Command Options | |
---|---|
-p | List currently defined aliases (only for alias command) |
-a | Clear all aliases (only for unalias command) |
Temporarily Assigning an Alias in Bash
When the alias command has been executed, and an alias has been created, it is available only for the current session. That is, the terminal window you have open or the login session you have with a remote server.
Exiting the terminal, logging out, rebooting, etc., will clear all aliases, and they will not be recreated for future sessions.
Example – Creating and Using an Alias
Here’s a simple example of defining an alias:
alias say_hello='echo "Hello LinuxScrew!"'
Above, an alias say_hello is defined, which will run the command:
echo "Hello LinuxScrew!"
But now, instead of having to type all of that out, the alias can be run instead:
say_hello
Even for this simple example, a lot of typing is saved. For more complex commands, aliases can save a lot of typing or remembering long command strings.
Permanently Assigning an Alias in Bash
Want to make an alias permanent so that it survives window closures, logouts, and reboots? Add your alias commands to the .bashrc file to reload them at each login:
nano ~/.bashrc
The .bashrc file defines the behavior of the Bash shell for your user account. Simply append your alias commands to the end of the file, one on each line, to execute those alias commands automatically each time you log in.
Listing Existing Alias
List existing aliases by running the alias command with the -p option:
alias -p
Removing an Alias
To remove an alias, use unalias:
unalias say_hello
Clearing All Alias
Clear all aliases by running the alias command with the -a option:
alias -a
Zsh Extras
While the above will work in the Zsh shell, Zsh also includes a bunch of other aliasing options:
http://zsh.sourceforge.net/Intro/intro_8.html
Zsh is increasing in popularity due to becoming the default shell in Apple’s macOS. However, Bash is still the default in most Linux operating systems, so I won’t delve too deeply into the additional functionality of Zsh to avoid confusion – that’s for another article!