alias command in Linux Explained

Last updated: December 24, 2022

Aliases in Linux are a convenient and time-saving tool for users who frequently use the command line. They allow users to create short, easy-to-remember commands that can be used in place of longer, more complex commands to execute certain tasks.

alias command Linux

The alias command in Linux is used to create and manage aliases, which are essentially shortcuts for longer commands. The alias command allows users to define a new name for a command, and then use that new name as a shortcut to execute Linux commands.

The alias command can also be used to define more advanced aliases, such as those that include multiple commands or arguments. For example, an alias could be created that combines several commands into one, or that includes variables or other elements.

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.

How to use alias command in Linux

To use the alias command and create alias, simply type "alias" followed by the new alias name and the command it will execute. This method can be used to create temporary aliases. For example:

alias la='ls -la'
create temporary aliases

This creates an alias called "la" that will execute the "ls -la" command when it is called. It will make it easy to use ls command line tool.

Check alias in Linux

There are several ways to check the aliases that have been defined in a Linux system:

Using alias command:

To view all defined aliases, simply type "alias" without any arguments:

alias alias ls='ls -la' alias grep='grep --color=auto'

Using alias command with -p flag:

To view the definitions of the aliases in a more readable format, use the "-p" option:

alias -p alias ls='ls -la' alias grep='grep --color=auto'

Using compgen command with -a flag:

To view all defined aliases, including those that may not be listed by the "alias" command, use the "compgen -a" command:

compgen -a alias ls='ls -la' alias grep='grep --color=auto'
list aliases

How to create aliases in Linux

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

alias l='ls -la'

This command can be used to create temporary alias called "l" that will execute the "ls -la" command when it is called.

Other examples of aliases that can be created include:

alias cl='clear'
alias up='cd ..'
alias h='history'

Permanent Alias in Bash

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:

bashrc

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.

Check where an alias was defined

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

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.

Linux alias command options

The alias command has only two options that can be used to modify its behavior:

  • -p: To display the definitions of the aliases in a more readable format.
  • -a: To display all defined aliases, including those that may not be listed by the "alias" command.

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.

Conclusion

Overall, aliases in Linux system are a powerful and convenient tool for users who frequently use the Linux command line. They allow users to create short, easy-to-remember commands that can save time and effort, and make it easier to remember how to execute certain tasks.

The alias command allows users to create and manage aliases, and the various options available provide additional flexibility and control. By using aliases, users can streamline their workflow and make the most of their time on the command line.

Navigate all-in-one place of Linux Commands for more learning.

SHARE

Comments

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

Leave a Reply

Leave a Comment