Introduction / Why This Is Useful
Aliases in Bash are one of the simplest and most effective ways to automate routine commands and reduce the time spent working in the terminal. Instead of typing a long chain of commands every time (e.g., sudo apt update && sudo apt upgrade -y), you create a short, memorable abbreviation (e.g., update).
After completing this guide, you will be able to:
- Create temporary aliases for the current terminal session.
- Configure permanent aliases that load automatically every time Bash starts.
- Use aliases with arguments via functions.
- Organize and manage your collection of aliases.
This is especially useful for developers, system administrators, and anyone who regularly works with the Linux command line.
Prerequisites / Preparation
Before you begin, ensure that:
- You have access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, Arch, etc.).
- You are familiar with basic commands (
cd,ls,nano/vim). - You have Bash installed (version 4.0 or newer). Check the version:
bash --version. - For some aliases (e.g., those using
sudo), your user must have the appropriate permissions insudoers.
Step-by-Step Instructions
Step 1: Understanding Alias Syntax
The basic syntax for the alias command is:
alias alias_name='command_with_arguments'
alias_name— a short word without spaces (e.g.,ll,gs,update).command_with_arguments— the full command or command chain you want to shorten. It is important to enclose it in quotes (single quotes' 'are preferable; they prevent premature variable substitution).- A space only between
aliasandalias_name. There should be no spaces betweenalias_nameand=, or between=and the quotes.
Example:
# Creates an alias 'll' that runs 'ls -la' with color highlighting
alias ll='ls -la --color=auto'
Step 2: Creating a Temporary Alias (for the Current Session)
The fastest way to test an alias is to create it directly in the terminal. It will work only while the current terminal is open.
- Enter the
aliascommand with your desired definition. For example, for Git:alias gs='git status' alias ga='git add' alias gc='git commit -m' - Use the new alias immediately:
gs # Will show the git repository status ga . # Will stage all changes gc "fix: updated readme" # Will create a commit with the message - Important: All temporary aliases will disappear after closing the terminal or opening a new one.
Step 3: Creating a Permanent Alias via ~/.bashrc
To make aliases persist between sessions, you need to add them to Bash's configuration file. The standard and most common file is ~/.bashrc.
- Open the
~/.bashrcfile in a text editor. Use whichever you prefer:nano ~/.bashrc # or vim ~/.bashrc # or, if VS Code is installed code ~/.bashrc - Scroll down the file and find a suitable place to add your aliases (e.g., at the very end or after the
# Aliasescomments). - Add your aliases, each on a new line. It is recommended to group them and add comments for clarity.
# --- My Useful Aliases --- # System alias update='sudo apt update && sudo apt upgrade -y' alias rebootnow='sudo reboot' # Navigation alias ..='cd ..' alias ...='cd ../..' alias c='clear' # Git (if you use it) alias gs='git status' alias gd='git diff' alias gl='git log --oneline --graph --all --decorate' - Save the file and close the editor (in
nano:Ctrl+X, thenYandEnter; invim::wq). - Apply the changes to the current session without restarting the terminal:
Now all added aliases are available. New aliases will also load automatically with every new terminal launch.source ~/.bashrc
Step 4: Using a Separate ~/.bash_aliases File (Recommended for Large Sets)
If you have many aliases, you can move them to a separate file to avoid cluttering the main ~/.bashrc.
- Create the
~/.bash_aliasesfile if it doesn't exist:touch ~/.bash_aliases - Open
~/.bashrcand ensure it contains the block that loads this file. It is usually already present but commented out. Find the lines:
Uncomment them (remove the# if [ -f ~/.bash_aliases ]; then # . ~/.bash_aliases # fi#at the start of each line) so it looks like:if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi - Save
~/.bashrcand runsource ~/.bashrc. - Now add all your aliases to the
~/.bash_aliasesfile. This is a cleaner solution.
Step 5: Creating Aliases with Arguments (Using Functions)
A simple alias cannot accept and process its own arguments ($1, $2, etc.). For this, use a wrapper function.
Example: An alias for quick searching in the current directory, where the first argument is the search text.
# In ~/.bashrc or ~/.bash_aliases
mygrep() {
# $1 - the first argument passed to the function
grep -r "$1" .
}
How to use:
mygrep "function_name" # Will find all occurrences of the string "function_name" in the current folder and subfolders
Example 2: An alias for quickly creating a directory and changing into it.
mkcd() {
mkdir -p "$1" && cd "$1"
}
Usage:
mkcd new_project # Will create the 'new_project' folder and navigate into it
Step 6: Managing and Removing Aliases
- View all active aliases (temporary and permanent, already loaded):
alias - Search for a specific alias:
alias | grep ll - Remove a temporary alias (from the current session):
To remove all aliases, you can useunalias llunalias -a(be careful!). - Remove a permanent alias: Delete the corresponding line from
~/.bashrcor~/.bash_aliasesand runsource ~/.bashrc(orsource ~/.bash_aliases).
Verifying the Result
- Create a test alias, e.g.,
alias hello='echo "Hello from FixPedia!"'. - If you added it to
~/.bashrc/~/.bash_aliases, runsourceon the relevant file. - Type
helloin the terminal. - The message
Hello from FixPedia!should appear on the screen. - Run
alias | grep hello— you should see your line.
If the alias does not work:
- Check that you didn't make a typo in the name.
- Ensure you ran
sourceafter editing the files. - Check the syntax: no spaces around
=, and the command is enclosed in quotes.
Potential Issues
alias: command not foundor alias doesn't work: You likely added the alias only to~/.bashrcbut didn't runsource ~/.bashrcin the current session. Or you are trying to use the alias in a script run withsh script.sh. By default, aliases do not work in non-interactive shells. For scripts, use functions or full commands.- Alias overrides an existing command: For example, you created
alias ls='ls -la', but later want to run the originallscommand. Use a backslash:\ls. - Problems with quotes and variables: If in your alias command you want a variable (e.g.,
$HOME) to be substituted at the time of use, not at the time of creation, use single quotes:alias mycd='cd $HOME/projects'. Usingalias mycd="cd $HOME/projects"(double quotes) will substitute$HOMEimmediately when you runsource ~/.bashrc. - Name conflicts: Ensure your alias name does not match an existing command or another alias. Use
type command_nameto check what it is. - Alias doesn't work with
sudo: By default,sudodoes not inherit aliases. To make an alias work withsudo, define it withsudoinside:alias update='sudo apt update'. Alternatively, usesudo !!to repeat the previous command withsudo.