Oct 15, 2023 4 min read

How to Create Bash Aliases

Create Bash Aliases with our step-by-step tutorial. Aliases in Bash is used to create a shortcut command for a longer command.

Create Bash Aliases
Table of Contents

Introduction

Before we begin talking about how to create bash aliases, let's briefly understand – What is a Bash Alias ?

You can use aliases in Bash to create a shortcut command for a longer command. When working on the command line, bash aliases are essentially shortcuts that can save you from having to remember long commands and eliminate a lot of typing. You could, for example, make the alias tgz a shortcut for the tar -xvfz command.

By using Bash alias, individuals can boost their productivity and simplify repetitive tasks. This unique functionality helps reduce typing effort and improves command-line navigation.

In this tutorial, you will use bash aliases. We will also address a few FAQs on how to use bash aliases.

Advantages of Bash Alias

  1. Efficiency: Bash aliases reduce the need to repeatedly type long or complex commands, saving time and effort.
  2. Productivity: Custom shortcuts enhance workflow and make command execution faster, improving overall efficiency.
  3. Simplification: Alias creation streamlines repetitive tasks, making them easier to perform with a shorter and more manageable syntax.
  4. Customization: Users can personalize their command-line experience by creating aliases that suit their preferred working style.
  5. Navigation: Bash aliases facilitate quick navigation through directories and files, enabling seamless movement within the command-line interface.

Creating Bash Aliases

Creating aliases in bash is a simple process. The following is the syntax:

alias alias_name="command_to_run"

The alias keyword is followed by the alias name, an equal sign, and the command to run when you type the alias. The command must be surrounded in quotes and the equal sign should not be spaced. Each alias should be stated on its own line.

One of the most commonly used commands on the Linux command line is ls. I normally use the -la parameter with this command to get a full list of all files and directories, even hidden ones.

Let's make an alias called ll, which will be a shortcut for the ls -la command. To do so, open a terminal window and type the following:

alias ll="ls -la"

Now, when you type ll in your terminal, you'll get the same result as if you typed ls -la.

Only the current shell session will have access to the ll alias. The alias will not be available if you end the session or start a new one from another terminal.

You must define the alias in the ~/.bash profile or ~/.bashrc file to make it persistent.

In your text editor, open the following file:

nano ~/.bashrc

as well as your aliases:

# Aliases
# alias alias_name="command_to_run"

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

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

The aliases should be given names that are simple to recall. It's also a good idea to leave a note for future reference.

Save and close the file when you're finished. Type the below command to make the aliases available in your current session:

source ~/.bashrc

As you can see, generating basic bash aliases is simple and quick.

ℹ️
You can save your aliases in a separate file to make your .bashrc more modular. Some Linux distributions, such as Ubuntu and Debian, include a .bash_aliases file that is derived from the ~/.bashrc file.

Creating Bash Aliases with Arguments (Bash Functions)

You may need to build an alias that accepts one or more arguments from time to time. Bash features come in handy in this situation.

Creating a bash function has a fairly simple syntax. They can be expressed in one of two ways:

function_name () {
  [commands]
}

or

function function_name {
  [commands]
}

Put any number of arguments following the function's name, separated by a space, to provide them to the bash function. The passed parameters are $1, $2, $3, and so on, corresponding to the parameter's position after the function's name. The function name is stored in the $0 variable.

Let's make a basic bash function that creates a directory and then navigates through it:

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

As with aliases, add the function to your ~/.bashrc file and reload the file with source ~/.bash profile.

Instead of typing mkdir to make a new directory and then cd to go into it, you can now just type:

mkcd new_directory

If you're not sure what -- and && are, here's a quick explanation.

  • --  - ensures that you don't unintentionally provide the command an extra argument. If you try to create a directory without using -- , for example, the directory name will be regarded as a command argument.
  • && - assures that the second command is only executed if the first one succeeds.

FAQs to Create Bash Aliases

Can I override existing commands with aliases? 

Yes, you can override existing commands with aliases. However, be cautious when doing so to avoid conflicts with essential system commands.

How can I view my current list of Bash aliases? 

Simply type alias in your terminal, and it will display a list of your current aliases along with their associated commands.

How do I make Bash aliases permanent? 

To make Bash aliases permanent, add them to your .bashrc or .bash_profile file. These files are sourced every time you open a new terminal session.

Can I use arguments with Bash aliases? 

Yes, you can use arguments with Bash aliases by using the $1, $2, and so on, to represent command line arguments within the alias command.

How can I remove a Bash alias?

 To remove a Bash alias, use the unalias command followed by the alias name. For example, unalias alias_name.

Can I share my Bash aliases with others? 

Yes, you can share your Bash aliases by exporting your alias file or by providing the alias commands to others, who can then add them to their own .bashrc or .bash_profile files.

Do Bash aliases work in other shells? 

Bash aliases work specifically in the Bash shell. Other shells may have different methods or syntax for creating aliases, such as csh using alias and ksh using function.

Conclusion

By now, you should have a good idea of how to develop bash aliases and functions that will make your life easier and more productive on the command line.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.