In the last post, I covered how to make bash aliases. However, creating bash aliases with the alias
command only lets you create bash aliases for the current session. For example, if I were to use alias hi="echo hi"
to make a bash alias that connects hi
with the command echo hi
, and then exited the terminal session, all the bash aliases would reset. However, there is a way to make them permanent.
Creating Permanent Bash aliases
To make Bash aliases permanent, you will need to add lines to the ~/.bash_profile
or ~/.bashrc
file. Once you find out that file, open it with the text editor of your choice. I prefer nano
.
Make sure to run the below command after you are done modifying those files:
BASH SHELL
source ~/.bash_profile
The below commands are how you get into the file.
BASH SHELL
cd ~
nano .bashrc
Then add your aliases.
BASH CONFIGURATION
# Aliases
# alias alias_name="command_to_run"
# If you are new to the shell, note that the Linux system ignores lines that start with '#' so this line does not mean anything to Linux
# Long format list
alias ll="ls -la"
# Print my public IP
alias myip="curl ipinfo.io/ip"
# That 'hi' example from earlier
alias hi="echo hi"
Now when you reboot your Linux system, the Bash aliases should still work.
Bash aliases with command arguments (Bash functions)
Sometimes you may need to create a Bash alias that will change depending on certain values or will accept arguments. The syntax for Bash functions is relatively easy. They can be declared in two separate formats. It is preferred to declare functions using the ~/.bashrc
file.
TIP: If you want your .bashrc
file to be more modular, you can store your aliases in a separate folder. Some distributions of Linux like Ubuntu or Debian include a separate .bash_aliases
file.
The syntax is below. Way 1 is the preferred way and the most used one.
Way 1 (Multi-Line):
BASH CONFIGURATION
function_name () {
commandsToRun
}
Way 1 (Single-Line):
BASH CONFIGURATION
function_name () { commandsToRun; }
Way 2 uses the keyword of function
, and then the function name, and after that the commands to run. The code below demonstrates way 2 (multi-line):
BASH CONFIGURATION
function function_name {
commandsToRun
}
Way 2 (Single-Line):
BASH CONFIGURATION
function function_name { commandsToRun; }
A few things that are nice to know:
- The commands that the function will run are between the curly braces (
{}
). This is called the body of the function. The curly braces must be separated from the body by newlines or spaces. In this case,function A {commandsToRun;}
orA () {commandsToRun;}
will not work, but something likefunction A { commandsToRun; }
orA () { commandsToRun; }
which has spaces separating the body of the function from the function data will. - Simply defining a function will not execute it. If you want to invoke or execute a bash function simply use the name of the function. The body of the function is executed when you invoke it in the shell script.
- You must define the function before calling or executing it.
You can pass any number of arguments in a function. If you want to call a Bash function right after the function’s name, separated by a space. The passed parameters can be referenced with $1
, $2
, $3
, $4
, etc., corresponding to the position of the argument after the name of the function when it is executed. Below is a simple bash function that will create a directory and then navigate into it:
BASH/SH
mkcd () {
mkdir -p -- "$1" && cd -p -- "$1"
}
Same as aliases, make sure to add the Bash functions in the ~/.bashrc
file and run source ~/.bash_profile
to make the changes take effect.
Now instead of having to make a directory using mkdir
and then move into that directory using cd
, you can simply use our mkcd
function:
BASH SHELL
mkcd testDirectory
A few things that you need to know:
- The
--
makes sure that Linux does not get confused and parse the folder name as an argument. If you have a folder that starts with-
, Linux might parse it as an argument. - The
&&
ensures that the second command runs only if the first one is successful.
Final Note
By now you should have a good understanding of how Bash aliases and functions work. This should help you be more productive on the command line.
If you have any questions or feedback, feel free to leave a comment on this post.