Linux

Creating and Managing Aliases in Bash: A Complete Guide

This guide explains what Bash aliases are and how to properly create and configure them to automate repetitive commands and boost terminal productivity in Linux.

Updated at February 16, 2026
10-15 min
Easy
FixPedia Team
Применимо к:Bash 4.0+Ubuntu 20.04+Debian 11+CentOS 8+Fedora 35+

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:

  1. You have access to a Linux terminal (Ubuntu, Debian, CentOS, Fedora, Arch, etc.).
  2. You are familiar with basic commands (cd, ls, nano/vim).
  3. You have Bash installed (version 4.0 or newer). Check the version: bash --version.
  4. For some aliases (e.g., those using sudo), your user must have the appropriate permissions in sudoers.

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 alias and alias_name. There should be no spaces between alias_name and =, 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.

  1. Enter the alias command with your desired definition. For example, for Git:
    alias gs='git status'
    alias ga='git add'
    alias gc='git commit -m'
    
  2. 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
    
  3. 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.

  1. Open the ~/.bashrc file in a text editor. Use whichever you prefer:
    nano ~/.bashrc
    # or
    vim ~/.bashrc
    # or, if VS Code is installed
    code ~/.bashrc
    
  2. Scroll down the file and find a suitable place to add your aliases (e.g., at the very end or after the # Aliases comments).
  3. 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'
    
  4. Save the file and close the editor (in nano: Ctrl+X, then Y and Enter; in vim: :wq).
  5. Apply the changes to the current session without restarting the terminal:
    source ~/.bashrc
    
    Now all added aliases are available. New aliases will also load automatically with every new terminal launch.

If you have many aliases, you can move them to a separate file to avoid cluttering the main ~/.bashrc.

  1. Create the ~/.bash_aliases file if it doesn't exist:
    touch ~/.bash_aliases
    
  2. Open ~/.bashrc and ensure it contains the block that loads this file. It is usually already present but commented out. Find the lines:
    # if [ -f ~/.bash_aliases ]; then
    #     . ~/.bash_aliases
    # fi
    
    Uncomment them (remove the # at the start of each line) so it looks like:
    if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
    fi
    
  3. Save ~/.bashrc and run source ~/.bashrc.
  4. Now add all your aliases to the ~/.bash_aliases file. 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):
    unalias ll
    
    To remove all aliases, you can use unalias -a (be careful!).
  • Remove a permanent alias: Delete the corresponding line from ~/.bashrc or ~/.bash_aliases and run source ~/.bashrc (or source ~/.bash_aliases).

Verifying the Result

  1. Create a test alias, e.g., alias hello='echo "Hello from FixPedia!"'.
  2. If you added it to ~/.bashrc/~/.bash_aliases, run source on the relevant file.
  3. Type hello in the terminal.
  4. The message Hello from FixPedia! should appear on the screen.
  5. 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 source after editing the files.
  • Check the syntax: no spaces around =, and the command is enclosed in quotes.

Potential Issues

  • alias: command not found or alias doesn't work: You likely added the alias only to ~/.bashrc but didn't run source ~/.bashrc in the current session. Or you are trying to use the alias in a script run with sh 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 original ls command. 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'. Using alias mycd="cd $HOME/projects" (double quotes) will substitute $HOME immediately when you run source ~/.bashrc.
  • Name conflicts: Ensure your alias name does not match an existing command or another alias. Use type command_name to check what it is.
  • Alias doesn't work with sudo: By default, sudo does not inherit aliases. To make an alias work with sudo, define it with sudo inside: alias update='sudo apt update'. Alternatively, use sudo !! to repeat the previous command with sudo.

F.A.Q.

My alias isn't working after restarting the terminal. What should I do?
Can I create an alias with arguments?
How do I view a list of all current aliases?
Why doesn't an alias with spaces or quotes work?

Hints

Understanding alias syntax
Creating a temporary alias
Creating a permanent alias via ~/.bashrc
Applying changes in ~/.bashrc
Using a separate file for aliases (optional)
Managing and removing aliases
FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community