Typing long or complex commands repeatedly can become tedious and inefficient for users who spend a significant amount of time in the Bash shell. Bash aliases provide a simple and effective solution to this problem.
This tutorial teaches you how to create and use Bash aliases.
What Is Bash Alias?
Bash alias is a mechanism for command substitution that enables users to personalize their Bash experience. Aliases enhance the command-line workflow by making it more efficient and reducing the likelihood of typing errors.
When creating an alias, the user replaces a longer command or sequence of commands with a shorter (or easier to remember) string. Each time the replacement string is called in the shell, Bash recognizes it as an alias and executes the original command.
How to Create Bash Alias
Create a Bash alias using the alias
command. Below is the basic command syntax:
alias [alias_name]='[original_command]'
For example, the following command defines ll
, an alias that tells Bash to execute the ls command with arguments -l
and -a
:
alias ll='ls -la'
When the user types ll
in the terminal, the shell interprets it as ls -la
and lists all files in the long format.
How to Create Permanent Bash Alias
Bash aliases created directly in the shell are temporary and exist only for the current session. Once the terminal window is closed, these aliases are lost.
To make aliases permanent, save their definitions in a shell initialization file. Common locations for these definitions include ~/.bashrc, ~/.bash_aliases, or ~/.profile. The .bashrc file is the most common and recommended place for alias definitions.
To make an alias permanent, follow these steps:
1. Open the chosen configuration file using a text editor such as Nano:
nano ~/.bashrc
2. Add the alias definition to a new line. Use the same syntax as when defining aliases on the command line.
3. Save the changes to the file and exit the editor.
Note: It is often helpful to group aliases or add them at the end of the file.
4. Source the file to apply changes immediately without restarting the terminal with:
source ~/.bashrc
How to Create Bash Alias with Arguments
Bash aliases are designed to perform a simple text substitution. For example, the user can define ll
as an alias for ls -la
, and then execute the following command:
ll [path]
Bash replaces ll
with ls -la
, and then appends [path]
to the resulting command, making it ls -la [path]
.
This property works for arguments appended to the end of the aliased command. However, to insert arguments into the middle of a command or use any logic with them, create a Bash function.
How Bash Functions Work
Bash functions are mini-scripts defined within a shell. They can accept arguments, use conditional logic, loops, etc., making them far more powerful than aliases.
Bash functions have the following syntax:
[function_name]() {
[command1]
[command2]
[...]
}
Arguments are accessed as $1, $2, $3, and so on, for each argument. For example, type the following to create a function named mcd
, which allows the user to create a directory and cd into it at the same time:
mcd() {
mkdir -p "$1" && cd "$1"
}
Execute the mcd
command to confirm the successful creation of the function:
mcd test
How to Use Bash Aliases
Once an alias is defined, either temporarily or permanently, type the alias name at the command prompt and press Enter. The shell expands the alias and executes the underlying command.
The sections below contain examples illustrating the capabilities of the Bash alias feature.
Updating System Packages
Keeping a system up to date is essential for both security and optimal performance. The following alias combines the commands to update the package lists and upgrade all installed packages on Debian-based systems:
alias update='sudo apt update && sudo apt upgrade -y'
Navigating to Home Directory
Users frequently need to return to their home directory from various locations in the file system. While cd ~
by itself also works, the following alias provides an explicit and memorable shortcut:
alias home='cd ~'
Displaying Disk Usage
Understanding disk space utilization is essential for system management. The alias below provides a summary of disk usage for the current directory in a format more readable than the default du command output.
alias dufree='du -sh'
Find Recent Files
Aliases can replace long and complex command strings that users execute every day. For example, use the following alias to find the files modified in the last 24 hours, excluding node modules and dotfiles:
alias find-recent='find . -type f -mtime -1 ! -path "*/node_modules/*" ! -name ".*" -print'
Quick Overview of System Resources
The alias below provides a comprehensive overview of display memory usage, disk space utilization, and CPU information by combining multiple diagnostic commands into one:
alias sysinfo='free -h && df -h / && lscpu | grep "Model name\|CPU(s)"'
Conclusion
After reading this tutorial, you can create and use Bash aliases to simplify your command-line workflow. The article also explained how to make aliases permanent and introduced Bash functions for more complex command substitutions.
Next, read how to run a Bash script.