How to Create Bash Aliases

By 

Updated on

6 min read

How to Create Bash Aliases

If you often find yourself typing long commands or searching your shell history for something you ran before, bash aliases can save you a great deal of time. Bash aliases let you define short, memorable shortcut commands that expand to longer ones.

For example, you could set the alias tgz as a shortcut for the tar -xzvf command .

This article explains how to create bash aliases so you can be more productive on the command line.

Quick Reference

For a printable quick reference, see the Bash cheatsheet .

CommandDescription
alias name="command"Define a temporary alias
aliasList all active aliases
unalias nameRemove a specific alias
unalias -aRemove all aliases in the current session
source ~/.bashrcReload .bashrc in the current session
type nameCheck whether a name is an alias or a command

Creating Bash Aliases

Creating aliases in bash is straightforward. The syntax is as follows:

txt
alias alias_name="command_to_run"

An alias declaration starts with the alias keyword followed by the alias name, an equal sign, and the command to run when you type the alias. The command must be enclosed in quotes with no spaces around the equal sign. Each alias must be declared on a new line.

The ls command is one of the most used commands on the Linux command line. We often use it with the -la flag to list all files and directories, including hidden ones, in long format.

To create a simple alias named ll as a shortcut for the ls -la command , open a terminal and type:

Terminal
alias ll="ls -la"

Now if you type ll, you will get the same output as ls -la.

The ll alias is available only for the current shell session. If you exit or open a new terminal, the alias will not be available.

Making Aliases Persistent

To make an alias persistent across sessions, declare it in ~/.bashrc . If you use a login shell, make sure ~/.bash_profile sources ~/.bashrc so the alias is available there too.

Before creating an alias, check whether the name conflicts with an existing command:

Terminal
type ll

If type reports that ll is already a command or alias, choose a different name.

Open ~/.bashrc in your text editor :

Terminal
nano ~/.bashrc

Add your aliases. It is good practice to add a comment above each alias for future reference:

~/.bashrcsh
# Aliases
# alias alias_name="command_to_run"

# Long format list
alias ll="ls -la"

# Print my public IP
alias myip='curl https://ipinfo.io/ip'

Save and close the file, then reload it in the current session with source :

Terminal
source ~/.bashrc
Tip
To keep your .bashrc modular, store aliases in a separate ~/.bash_aliases file. Ubuntu and Debian include this file by default and source it automatically from ~/.bashrc.

Removing Aliases

To remove an alias from the current session, use unalias:

Terminal
unalias ll

To remove all active aliases at once:

Terminal
unalias -a

To permanently remove an alias, delete or comment out its line in ~/.bashrc, then run source ~/.bashrc.

Creating Bash Aliases with Arguments (Bash Functions)

Bash aliases cannot define or rearrange positional arguments. Extra words typed after an alias are appended to the expanded command, but when you need a shortcut that handles input with $1, $2, or custom logic, use a bash function instead.

Functions can be declared in two formats:

txt
function_name () {
  [commands]
}

or

txt
function function_name {
  [commands]
}

To pass arguments to a bash function, place them after the function name separated by spaces. Inside the function, $1 refers to the first argument, $2 to the second, and so on. $0 refers to the shell or script name, while ${FUNCNAME[0]} gives you the current function name.

Here is a function that creates a directory and immediately navigates into it:

~/.bashrcsh
mkcd ()
{
  mkdir -p -- "$1" && cd -P -- "$1"
}

Add the function to your ~/.bashrc and run source ~/.bashrc to load it. Now instead of running mkdir followed by cd, you can type:

Terminal
mkcd new_directory

The -- and && in the function serve specific purposes:

  • -- - signals the end of options, preventing a directory name starting with - from being interpreted as a flag
  • && - ensures the cd command runs only if mkdir succeeds

Quick Tips

To temporarily bypass an alias and run the original command, prefix it with a backslash:

Terminal
\ls

To see the definition of an existing alias:

Terminal
type ll

Troubleshooting

Alias is not available after adding it to .bashrc
You need to reload the file after editing it. Run source ~/.bashrc in the current terminal. New sessions will pick up the alias automatically.

You are using Zsh, not Bash
If your shell is Zsh, add aliases to ~/.zshrc instead of ~/.bashrc, then run source ~/.zshrc or open a new terminal session.

Alias does not work inside a shell script
Bash does not expand aliases in non-interactive scripts by default. Use a shell function instead, or add shopt -s expand_aliases at the top of the script before the alias declaration.

Alias name shadows a system command
If an alias shares a name with an existing command, the alias takes precedence. Run type name before defining an alias to check for conflicts. To bypass an alias temporarily, use \name.

FAQ

How do I list all currently defined aliases?
Run alias without any arguments. It prints all active aliases in the current session in alias name='command' format.

How do I remove an alias?
Use unalias alias_name to remove a single alias from the current session. To make the removal permanent, delete the corresponding line from ~/.bashrc and run source ~/.bashrc.

Why do aliases not work in shell scripts?
Aliases are only expanded in interactive shells. In scripts, use shell functions instead. If you must use aliases in a script, add shopt -s expand_aliases before defining them.

How do I temporarily run the original command when an alias is defined?
Prefix the command with a backslash: \ls. This bypasses the alias and runs the original binary directly.

Can I use single quotes and double quotes interchangeably in an alias?
Both work, but they behave differently with variable expansion. Double quotes expand variables when the alias is defined; single quotes expand them when the alias is run. For most aliases, single quotes are safer.

Conclusion

Bash aliases are a simple way to shorten repetitive commands and make the command line faster to work with. Store your aliases in ~/.bashrc or ~/.bash_aliases and reload the file with source ~/.bashrc after each change.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page