How to create a Bash alias
Do you often find yourself typing a long command on the command line or searching your bash history for a previously typed command
How often do you find yourself typing a long command on the command line or searching the bash history for a previously typed command?
If you answered yes to all of these questions, you'll find bash aliases handy. Bash aliases allow you to set up easy-to-remember shortcut commands for long commands.
The Bash alias is essentially a shortcut that saves you from having to remember long commands and eliminates a lot of typing when using the command line.
For example, you can set a shortcut to tgz
for the alias of the tar -xvfz
command. This tutorial shows you how to create bash aliases to improve your productivity on the command line.
In bash, creating an alias is very simple. This is the syntax for creating an alias by alias alias_name="command_to_run"
.
alias alias_name="command_to_run"
The alias keyword is used to create bash aliases. This is followed by the name of the declaration alias, followed by an equal sign and the command you want to run.
Commands need to be enclosed in quotation marks and have no spacing around the equals sign. Each alias needs to be declared on a new line.
The ls command is probably one of the most commonly used commands on the Linux command line. I usually use the ls
command with the -la
option to list all files and directories, hidden files and directories in long list format.
Now let's create the bash alias ll
for the ls -al
command, which will become a shortcut to the ls -la
command. Create bash alias by running the command alias ll="ls -la"
on your terminal.
alias ll="ls -la"
Now, if you type ll in the terminal, you will get the same output as if you typed ls-la.
ll aliases created using the alias command are only available for the current shell session. If you exit a shell session or open a new session from another terminal, the alias ll is not available.
To make an alias persistent, you need to declare it in a ~/.bash_profile
or ~/.bashrc
file. If you are using Zsh, the corresponding configuration file is ~/.zshrc
.
Use your favorite text editor. In this tutorial, we will open the file ~/.bashrc
using vim.
vim ~/.bashrc
You should use an easy-to-remember name for the alias. We suggest you annotate for future reference. When you are done, save the file and exit vim.
Now that we have modified the Bash configuration file, we need to load the bash configuration file into the current session to make the alias available for the current shell session. Run the source ~/.bashrc
command.
source ~/.bashrc
As you can see, creating a simple bash alias is quick and easy. If you want to make.bashrc more modular, you can store the alias in a separate.bash_aliases file.
Finally, use the source command in the ~/.bashrc
file to load the.bash_aliases file.
Bash alias parameter
Sometimes you might want to create an alias that takes one or more arguments, which is where you need to use the bash function. The syntax for creating bash functions is very simple. They can be declared in two different formats.
To pass any number of parameters to a bash function, you simply run the function with the parameters after the function name, separated by Spaces.
In the function, you can use the format $1
, $2
, $3
to get the parameters specified when running the function, 1.... 100 corresponds to the position after the parameter function name. The $0
variable is reserved for the function name.
function_name () {
[commands]
}
function function_name {
[commands]
}
Now create a simple bash function mkcd, which will create a directory with its parameters, and then run the cd command to switch to that directory.
As with creating the alias, you also need to run source ~/.bashrc
to load the mkcd function after adding the function to the ~/.bashrc
file.
Now, run the mkcd function, and mkcd will automatically create the directory and switch to it.
source ~/.bashrc
mkcd new_directory
If you want to know what is --
&&
here's a short explanation.
Make sure you don't accidentally pass extra parameters to the command. && ensures that the second command is only run if the first command succeeds.
By now, you should have a good understanding of how to create bash aliases and functions, which will make it easier and more efficient for you to use on the command line.
If you have any questions or feedback, please feel free to comment.