How to Create and Use Bash Alias

Updated on November 15, 2024
How to Create and Use Bash Alias header image

Introduction

A Bash alias is a shortcut for substituting long commands with shorter commands to save time and avoid repetitive typing. In a shell environment, you should store Bash aliases in the .bashrc file under the user's home directory.

This article explains how to create and use Bash aliases.

The Bash Alias Syntax

The following is a basic Bash alias syntax:

alias alias-name="command"

In the above command:

  • alias: Creates a new alias command.
  • alias-name: Specifies the alias name.
  • command: Sets the command that you want to substitute. Enclose the command using single or double quotes if the command includes spaces or special characters to prevent shell interpretation.

Create Bash Aliases

Follow the steps below to create and use Bash aliases in a shell environment.

  1. Create a sample alias application, such as hello-world that runs the echo command.

    console
    $ alias hello-world='echo "Hello, World! Greetings from Vultr."'
    
  2. Run the hello-world command to display the result.

    console
    $ hello-world
    

    Output:

    Hello, World! Greetings from Vultr.
  3. Create another list-files alias to substitute the ls command.

    console
    $ alias list-files='ls'
    
  4. Run the list-files command.

    console
    $ list-files
    

    Verify that the command lists all files in your working directory.

Create Permanent Bash Aliases

Bash aliases are temporary in a shell environment unless you save them in the .bashrc file. Follow the steps below to make the Bash aliases permanent.

  1. Switch to your user's home directory.

    console
    $ cd
    
  2. Create a new myaliases file using a text editor like nano.

    console
    $ nano myaliases
    
  3. Add the following directives to the myaliases file.

    bash
    alias hello-world='echo'
    alias list-files='ls'
    alias newfile='touch'
    

    Save and close the file.

    The hello-world, list-files, and newfile aliases in the above configuration execute the echo, ls, and touch commands respectively.

  4. Open the .bashrc file.

    console
    $ nano .bashrc
    
  5. Add the following directives at the end of the file.

    bash
    if [ -f myaliases ]; then
    . myaliases
    fi
    

    Save and close the file.

    The above if-then statement finds the myaliases file in the user's home directory and runs it to load all aliases in the .bashrc file.

  6. Apply the .bashrc changes to your shell environment.

    console
    $ source .bashrc
    
  7. Run the newfile alias command and specify sample-file as an argument to create a new file.

    console
    $ newfile sample-file
    
  8. List files using the list-file alias in your working directory and verify that sample-file is available.

    console
    $ list-files
    

    Output:

    ... sample-file ....
  9. Run the hello-world alias and specify Greetings from Vultr as an option.

    console
    $ hello-world Greetings from Vultr
    

    Output:

    Greetings from Vultr

Use Bash Aliases with Functions and Arguments

You can combine aliases with command arguments and functions in Bash scripts to perform specific tasks, such as creating and managing files. Follow the steps below.

  1. Create a new bashalias.sh script.

    console
    $ nano bashalias.sh
    
  2. Add the following contents to the bashalias.sh file.

    bash
    #!/bin/bash
    
    myfunction ()
    {
    mkdir -p "$1" && cd "$1"
    touch hello_bash
    }
    
    myfunction $1
    
    echo "New Directory and File Created."
    

    Save and close the file.

    In the above script the myfunction function works as an alias for the mkdir, cd, and touch commands to create a new directory. The function then switches to the directory and creates a new hello_bash file. You can convert myfunction to a global alias command by appending it to the .bashrc file.

  3. Run the script using Bash and specify test_directory as the command option.

    console
    $ bash bashalias.sh test_directory
    
  4. List the files in the test_directory.

    console
    $ ls test_directory/
    

    Output:

    hello_world

Use Bash Alias in Bash Script

You can use Bash aliases in scripts to shorten long commands and allow code reusability with other Bash features such as conditional statements and loops. Follow the steps below to use aliases in a Bash script to validate and run specific tasks.

  1. Create a new advanced-alias.sh script.

    console
    $ nano advanced-alias.sh
    
  2. Add the following contents to the advanced-alias.sh file.

    bash
    #!/bin/bash
    
    shopt -s expand_aliases
    
    alias sysinfo='uname -a && lscpu && free -h'                
    alias lamp='sudo apt update && sudo apt install apache mysql php'                         
    alias lemp='sudo apt update && sudo apt install nginx mysql php'                    
    alias mean='sudo apt update && sudo apt install -y mongodb npm && sudo npm install -g @angular/cli && sudo npm'  
    
    echo "Select an option:"
    echo "1. Show System Information"
    echo "2. Install LAMP"
    echo "3. Install LEMP"
    echo "4. Install MEAN"
    read -p "Enter your selection (1-4): " selection
    
    if [ "$selection" -eq 1 ]; then
        echo -e "\nGathering system information..."
        sysinfo
    
    elif [ "$selection" -eq 2 ]; then
        echo -e "\n Installing the LAMP Stack"
        lamp
    
    elif [ "$selection" -eq 3 ]; then
        echo -e "\n Installing LEMP"
        lemp
    
    elif [ "$selection" -eq 4 ]; then
        echo -e "\nInstalling MEAN:"
        mean
    
    else
        echo "Invalid selection. Please select a valid option."
        exit 1
    fi
    

    Save and close the file.

    The shopt -s expand_aliases command in the above script enables alias expansion while the lamp, lemp, and mean Bash aliases shorten the full installation commands to install specific applications. The selection variable stores the user input and uses the if-elif conditional statement to run the Bash alias commands.

  3. Run the script using Bash.

    console
    $ bash advanced-alias.sh
    
  4. Enter a selection such as 2 to run the lamp alias command.

    Select an option:
    1. Show System Information
    2. Install LAMP
    3. Install LEMP
    4. Install MEAN
    Enter your selection (1-4): 2

    Output:

    Installing the LAMP Stack
    .................................

Conclusion

You have used aliases in a Bash shell environment to create reusable commands. The bash alias command allows you to run multiple commands, utilizing advanced Bash features such as conditional statements and loops to perform specific tasks.