There are the majority of commands that we use while operating the command-line interface. Most of the commands are habitual, and people may run those commands in the same way every day. However, we have an option in Bash to create our own shortcuts with the help of aliases, which will eliminate the extraneous typing by using shortened names.
Bash Alias is used to set a shortcut command for a longer command. The alias command allows us to launch any command or set commands by using a single word. For example, we can set the command 'cc' as a shortcut for the 'clear' command. Using 'cc + enter' is comparatively much faster than to type 'clear' command.
Alias is usually declared within the ~/.bash_profile or ~/.bashrc file.
"A .bash_profile and .bashrc files are referred to as configuration files for the bash shell. All the bash configurations, such as all the terminal sessions, the configuration comprising of environment variables, default directory, color, bash theme, etc., are stored in the configuration file. The name of the configuration file is usually ".bashrc" for a terminal-sessions and ".bash_profile" for login shells."
Bash Alias Structure
A bash alias contains the following structure:
A new alias is defined on a new line with the 'alias' keyword. We need to define the shortcut command that we want to use with the alias name, followed by an equal sign. Then, we type the full command that we want to run within the quotes. There should be no spacing between the neighbor elements and the equal sign. It's an important thing to remember; otherwise, the command will be broken.
Create a Bash Alias
Creating aliases in bash is straight forward. We can declare the aliases into the command line by following the structure shown above.
Let's start with a simple bash alias now. One of the commonly used commands that many people use to get the listing of all the files and directory, including hidden files, is "ls -la". We can create a shortcut "ll' to perform an action of "ls -la" by typing the following command in a terminal:
Now, if we type the alias "ll" in a terminal, we will receive the listing of all the files and directories in a long format as similar to the "ls -la" command.
Note: It should be noted that if we set the aliases through a terminal using this way, aliases will only be available for the current shell session. When we open a new terminal window, aliases will not be available.
If we want to make the defined aliases persistent, we have to add this into one of the files which are read when a shell session starts. The most common choices are ~/.bash_profile or ~/.bashrc, as we have mentioned earlier. We are required to open any of these files and add the aliases there.
It is a good practice to assign such names for the aliases, which are easy to remember. It is also suggested to add a comment declaring an entire function related to bash aliases for future reference.
If we want to make our .bashrc file more modular, then we can put the aliases in a separate file, i.e., ~/.bash_aliases. We need to make sure that the code appears in the ~/.bashrc file:
Remove/Delete a Bash Alias
To remove the alias, we are required to use the following structure:
To remove the "ll" alias that we have created above, we can use the unalias command:
The "ll" alias will be removed.
If aliases are declared in ~/.bash_profile or ~/.bashrc, simply edit the file using a text editor and remove those aliases from there.
List Bash Aliases
We can list all the configured aliases by using the "alias" command in a terminal without any arguments:
It will look like this:
Bash Aliases with Arguments (Bash Functions)
In some cases, we may require the aliases which accept one or more argument. In such cases, bash functions are useful.
Following is the syntax for creating bash functions. It can be declared in two different formats:
To pass any number of arguments to the bash function, we can simply put them right after the function's name separated by a space. The passed parameters can be $1, $2, $3, etc. It usually depends upon the corresponding position of the parameter after the function's name. The $0 variable is kept reserved for the function name.
Now, we are going to create a simple bash function, which will create a directory and then navigate into it without using 'mkdir' and 'cd' command:
Just like aliases, we need to add the function to the ~/.bashrc file and run source~/.bash_profile to reload the file. Here, AND Operator (&&) ensures that the second command runs only if the first command is executed successfully. And double dash sign (--) ensures that we are not passing an extra argument to the command.
Now, we can create a new directory and then move into that directory using the command:
Hence, the aliases are an excellent alternative to reduce the amount of typing for the long commands repeatedly.