# Understanding Bash Aliases in Linux

In Linux, [aliases](https://phoenixnap.com/kb/linux-alias-command#:~:text=What%20Is%20an%20Alias%20in%20Linux%3F) are shortcuts that replace long or frequently used commands with shorter, more convenient ones, improving efficiency by speeding up the workflow and avoiding potential spelling errors. They can also replace commands with additional options, making them easier to use.

## Alias Benefits

* Save time by typing less.
    
* Improve command memorability through custom names.
    
* Increase efficiency by automating repetitive tasks.
    

## Linux Alias Syntax

To create an alias, use the `alias` command and its syntax as follows:

```bash
alias [name]='[value]'
```

* `alias`: Invokes the `alias` command.
    
* `[name]`: the name of the alias is being created. This is what will be typed in the terminal instead of the full command. A name is a user-defined string, excluding special characters and **'alias'**, **'unalias'** keywords that cannot be used as names.
    
* `[value]`: the actual command that the alias represents. This can be a simple command or a more complex command chain. Commands can also include options, arguments, and variables.
    

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>Note:</strong> Enclosing the value in single quotation marks (<strong>'</strong>) <strong>will not expand </strong>any variables used with the command. To expand the variables, use double quotation marks (<strong>"</strong>).</div>
</div>

## **Create Aliases in Linux**

There are two types of aliases to create in Linux:

* **Temporary**. Add them using the `alias` command.
    
* **Permanent**. These require editing the system files.
    

### Create a Temporary Alias

Aliases defined directly in the terminal are temporary and last only for the current terminal session. Use the `alias` command to create a temporary alias. For example, adding `move` as an alias for the `mv` command with the option of asking for confirmation before overwriting:

```bash
alias move='mv -i'
```

Another use for aliases is to create a shortcut for running scripts. To do this, provide the [absolute path to the scrip](https://phoenixnap.com/glossary/absolute-path)t as the value:

```bash
alias frename='Example/Test/file_rename.sh'
```

In this example, using `frename` as a command runs the `file_rename.sh` bash script.

### Create a Permanent Alias

To make an alias permanent, add it to the shell's configuration file, such as `~/.bashrc` or `~/.bash_profile`, and then source the file or restart your terminal. Depending on the type of shell you are using, use:

* Bash shell: *~/.bashrc*
    
* Zsh shell: *~/.zshrc*
    
* Fish shell: *~/.config/fish/config.fish*
    

Start by opening the shell configuration file in a text editor. In this example, we are using the Bash shell and vim text editor:

```bash
sudo vim ~/.bashrc
```

Scroll down until you find a section that lists default system aliases. For ease of use, create a separate section with a descriptive comment and add your aliases using the `alias` command syntax.

In our example:

```bash
#Custom aliases
alias c='clear';
alias df='df -h -x squashfs -x tmpfs -x devtmpfs'
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710081825534/698c44ca-b4cb-4bba-a0dc-6df713c68c8c.png align="center")

Once all of the new aliases are added, press **ESC,** type **:wq!** to save the changes to the configuration file.

The new aliases automatically load in the next terminal session. If you want to use them in the current session, load the configuration file using the `source` command:

```bash
source ~/.bashrc
```

The output of the `df` command before creating an alias for it:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710082427680/5b7727b1-7a6d-4481-86a5-3487c8fc4272.png align="center")

The output of the `df` command after creating an alias for it:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710082503420/4278631c-cfc0-438d-a2e8-0ec6ffb8522a.png align="center")

## **List Aliases in Linux**

Using the `alias` command on its own displays a list of all currently set aliases:

```bash
alias
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710082843480/cbd470c4-4a65-4e58-b40e-dff5893ac283.png align="center")

## **Remove Aliases in Linux**

To remove an alias, use the `unalias` command with the following syntax:

```bash
unalias [name]
```

Adding the `-a` option allows to remove all aliases:

```bash
unalias -a
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1710083117013/4f8f8487-2c5f-498c-9c5f-a11a4cb85ae2.png align="center")

## References:

1. [**Linux Cra**](https://www.youtube.com/watch?v=Ok_kD_sgNcs&list=PLT98CRl2KxKHKd_tH3ssq0HPrThx2hESW&index=9)[**sh Course - B**](https://phoenixnap.com/glossary/absolute-path)[**ash Aliases**](https://www.youtube.com/watch?v=Ok_kD_sgNcs&list=PLT98CRl2KxKHKd_tH3ssq0HPrThx2hESW&index=9)
    
2. [**Bash aliases you can’t live without**](https://opensource.com/article/19/7/bash-aliases)
    
3. [Linux alias Command: How to Use It With Examples](https://phoenixnap.com/kb/linux-alias-command)
    
4. [**Linux Commands Cheat Sheet**](https://phoenixnap.com/kb/linux-commands-cheat-sheet)
