alias command in Linux Explained

Last updated: February 19, 2024 | Linuxopsys

The alias command in Linux is used to create shortcuts or alternate names for longer commands. This can help you save time by allowing you to create shorter, more memorable commands for frequently used tasks.

Syntax

The syntax for the alias command is as follows:

alias alias-name='command'

Where alias-name is the new name for the command, and 'command' is the command that the alias executes.

Using alias command

Creating aliases:

To create aliases in Linux, simply type alias command followed by the new alias name and the command it will execute. For example:

alias l='ls -la'

This creates an alias named "l" that will execute the "ls -la" command when it is called.

Listing aliases:

To view all defined aliases, simply type alias without any arguments. It will display the list of aliases in the format alias name=value.

$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

If you use the -p option explicitly (alias -p), it will also print the list of aliases in the format alias name=value.

When arguments are supplied, the command attached to each alias name is displayed. For example:

$ alias ls
alias ls='ls --color=auto'

or
$ alias ls la
alias ls='ls --color=auto'
alias la='ls -A'

Alternatively, you may also use type command followed by alias name. Example:

$ type ls
ls is aliased to `ls --color=auto'

If you want to list all alias names alone, then compgen -a is an option.

Remember you can't use arguments directly to alias name, the workaround would be to use functions.

Remove Alias:

To remove an alias, you can use the unalias command followed by the name of the alias you want to remove. For example:

unalias alias_name

This will remove the alias with the name alias_name from your system.

Note that the unalias command is only available in shells like bash and zsh. If you are using a different shell, you may need to use a different method to remove aliases.

Making Aliases Permanent:

To make an alias permanent in Bash, it must be added to one of the Bash configuration files. The rest process is same as temporary alias. The most common configuration files are:

The bashrc file is located in the home directory and is executed every time a new Bash shell is opened. To create alias in this file, simply open it in a text editor and add the alias definition to the end of the file.

Set permanent aliases in bash

~/.bash_profile

The ".bash_profile" configuration file is also located in the home directory and is executed when a user logs in to the system. To create alias in this file, simply open it in a text editor and add the alias definition to the end of the file.

To apply the changes made to either of these files, use the "source" command followed by the name of the file:

source ~/.bashrc
or
source ~/.bash_profile

Note: If you receive the error "bash: alias: alias: not found", this may be due to a typo or other error in the alias definition. Make sure that the alias definition is correct and does not contain any spaces or other invalid characters.

To check where an alias was defined, you can use the "grep" command to search through every Bash configuration file. For example:

grep -rl alias ~/.bash* ~/.profile /etc/profile /etc/bash.bashrc

This will search for any files that contain the word "alias" and display the file names and locations.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment