How to Set Aliases in Bash

How to Set Aliases in Bash

If you spend any real time in a terminal, you already know the feeling of typing the same long command for the tenth time in a day and thinking, “there has to be a shorter way.” There is, and it’s called an alias. I’ve been using Bash aliases for years, and they’re still one of the first things I set up on any new machine because they save so much repetitive typing and reduce the chance of typos in commands I run constantly.

In this guide, I’ll walk you through everything about Bash aliases — what they are, how to create them, how to make them permanent, and how to use them in real workflows. Whether you’re just starting out with the command line or you’re looking to tighten up your existing setup, this should give you a solid, practical foundation.

What Is a Bash Alias?

An alias is simply a shortcut you define for a longer command or a sequence of commands. Instead of typing ls -la every time you want a detailed directory listing, you can create an alias called ll that does the same thing. When you type ll in your terminal, Bash substitutes it with ls -la before running it.

Aliases don’t create new functionality — they just save you keystrokes and help you remember commands you use often but might otherwise forget the exact flags for.

Basic Syntax

The syntax for creating an alias is straightforward:

alias name='command'

For example:

alias ll='ls -la'

Now, whenever I type ll in this terminal session, Bash runs ls -la instead.

A few syntax notes worth knowing:

  • There should be no spaces around the = sign.
  • The command should be wrapped in quotes if it contains spaces or special characters.
  • Alias names typically use letters, numbers, underscores, and hyphens — avoid special characters that Bash might interpret differently.

Creating Your First Alias

Let’s start simple. Open your terminal and type:

alias greet='echo "Hello from your terminal!"'

Now run:

greet

Output:

Hello from your terminal!

That’s it — you’ve created and used your first alias. But there’s a catch: this alias only exists for the current terminal session. Close the terminal, and it’s gone. I’ll show you how to fix that shortly.

Practical Aliases I Actually Use

Here are some aliases that solve real annoyances:

alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
alias ..='cd ..'
alias ...='cd ../..'
alias grep='grep --color=auto'
alias c='clear'
alias h='history'
alias df='df -h'
alias du='du -h'

Let me break down why these matter:

  • ll, la, l — different flavors of directory listing depending on how much detail I want.
  • .. and ... — quick navigation up one or two directories without typing cd .. twice.
  • grep='grep --color=auto' — highlights matches in search results, which makes scanning output much faster.
  • df -h and du -h — show disk usage in human-readable format (GB/MB) instead of raw byte counts.

Aliases With Arguments? Not Quite — Here’s the Nuance

One common misconception is that aliases can accept arguments the way functions do. Technically, aliases can accept arguments appended at the end, but they can’t insert an argument in the middle of the command. For example:

alias mkdir='mkdir -p'

If I run mkdir -p projects/new, the argument projects/new gets appended after mkdir -p, so it works fine. But if you need more complex logic — like inserting a variable in the middle of a command — you actually want a Bash function instead of an alias. I’ll touch on that later.

Making Aliases Permanent

Since aliases created directly in the terminal disappear when you close the session, you need to store them in a configuration file that Bash reads every time it starts.

Step 1: Open Your Bash Configuration File

Depending on your system, this is usually ~/.bashrc (for interactive non-login shells) or ~/.bash_profile / ~/.profile (for login shells). On most Linux distributions, ~/.bashrc is the one that matters for everyday terminal sessions.

nano ~/.bashrc

Step 2: Add Your Aliases

Scroll to the bottom of the file and add your aliases:

# Custom aliases
alias ll='ls -la'
alias update='sudo apt update && sudo apt upgrade -y'
alias ports='netstat -tulanp'

Step 3: Reload the Configuration

Save the file, then reload it without restarting your terminal:

source ~/.bashrc

This tells Bash to re-read the file immediately, so your new aliases are available right away.

Organizing Aliases in a Separate File

As my list of aliases grew, I found it messy to keep them all inside .bashrc directly. A cleaner approach is to keep them in a dedicated file and source that file from .bashrc.

Create a new file:

nano ~/.bash_aliases

Add your aliases there, then in ~/.bashrc, add this snippet (many distributions already include it by default):

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

This keeps your main configuration file clean and makes it easy to back up or share just your aliases.

Viewing and Removing Aliases

To see all currently active aliases:

alias

To check a specific one:

alias ll

To remove an alias for the current session:

unalias ll

If it’s defined in .bashrc or .bash_aliases, you’ll need to remove the line from that file and reload the config for it to be gone permanently.

Real-World Use Cases

System administration: I use aliases like alias sshprod='ssh user@production-server.example.com' to avoid retyping long hostnames and usernames.

Git shortcuts:

alias gs='git status'
alias ga='git add .'
alias gc='git commit -m'
alias gp='git push'

Safety nets:

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

These add a confirmation prompt before deleting, copying, or moving files — a small habit that has saved me from accidental deletions more than once.

Docker shortcuts:

alias dps='docker ps'
alias dimg='docker images'
alias dstop='docker stop $(docker ps -q)'

Aliases vs. Functions

If you need conditional logic, multiple commands, or the ability to insert arguments anywhere in the command, a Bash function is the better tool:

mkcd() {
    mkdir -p "$1" && cd "$1"
}

This creates a directory and moves into it in one step — something a plain alias can’t do cleanly. Functions also go in .bashrc and work alongside your aliases.

Best Practices

  • Keep alias names short but memorable — don’t sacrifice clarity for brevity.
  • Avoid overriding critical commands (like ls or cd) without an escape hatch. If you alias rm to rm -i, remember you can still bypass it with \rm when needed.
  • Group related aliases with comments so your config file stays readable.
  • Don’t create aliases for commands you rarely use — it adds mental overhead without real benefit.
  • Sync your .bash_aliases file across machines using a dotfiles repository on GitHub so your setup is consistent everywhere.

Security Considerations

Aliases run with the same permissions as your shell, so be cautious about:

  • Aliasing common commands to something destructive by accident — always test new aliases before adding them permanently.
  • Sourcing alias files from untrusted sources. If you copy someone else’s .bashrc or .bash_aliases, review it first, since a malicious alias could silently redefine common commands like sudo or ls.
  • Avoiding aliases that embed passwords or sensitive tokens directly in plaintext, since .bashrc is often readable by other processes running as your user.

Troubleshooting Common Issues

Alias not working after adding it to .bashrc: Run source ~/.bashrc or open a new terminal tab. Bash only reads this file at shell startup unless you manually reload it.

Alias works in interactive shell but not in scripts: By default, aliases aren’t expanded in non-interactive shells or scripts. If you need alias-like behavior in scripts, use functions instead.

Alias silently overridden: Check if the same alias name is defined more than once in your configuration files — the last one defined wins.

“command not found” after unaliasing: Make sure you’re not accidentally removing the underlying command too. unalias only removes the alias, not the actual program.

Frequently Asked Questions

Do aliases work in all shells? No, aliases as shown here are specific to Bash syntax. Zsh supports similar syntax, but other shells like Fish use different conventions.

Can I chain multiple commands in one alias? Yes, using && or ;. For example: alias update='sudo apt update && sudo apt upgrade -y'.

Will aliases slow down my shell? No, aliases have negligible performance impact even in large numbers.

Can I alias a command to itself with extra flags? Yes, this is common, like alias ls='ls --color=auto'.

How do I make an alias available to all users on a system? Add it to /etc/bash.bashrc or /etc/profile.d/ instead of a user’s personal .bashrc.

Common Mistakes to Avoid

  • Forgetting to reload .bashrc after making changes and assuming the alias isn’t working.
  • Using single quotes when you actually need variable expansion (single quotes prevent expansion, double quotes allow it).
  • Overwriting essential commands without a way to bypass them.
  • Not documenting why an alias exists, leading to confusion months later.

Summary

Bash aliases are one of the simplest but most effective ways to speed up your command-line workflow. Once you define them in ~/.bashrc or a dedicated ~/.bash_aliases file, they become a permanent part of your environment, saving you time on every session. Start with a handful of aliases for commands you use daily, keep them organized, and expand to functions when you need more flexibility than a simple substitution allows.

References

Total
2
Shares

Leave a Reply

Previous Post
How to Customize Your Bash Prompt

How to Customize Your Bash Prompt

Next Post
How to Use History Commands in Bash

How to Use History Commands in Bash

Related Posts