Table of Contents
This tip is about the how to set up and use aliases in Linux shell commands. So read this free guide, How to set up and use aliases in Linux shell commands step by step. If you have query related to same article you may contact us.
How to set up and use aliases in Linux shell commands – Guide
An alias is known as a short name that the shell translates into another name or command. Aliases help describe new commands by exchanging a string for the first token of a simple command. They are commonly placed in ~ / .bashrc (bash) or ~ / .tcshrc (tcsh) startup files so that they are available to interactive subshells.
In bash, the built-in alias syntax is
surname [name[=value]]
In tcsh, the syntax is
surname [name[ value]]
In bash syntax, no spaces are allowed around the equal sign. If the value has spaces or tabs, you must enclose it in quotation marks. Unlike aliases in tcsh, a bash alias does not accept a command-line argument in value. Use a bash function when you need to take an argument.
An alias is not replaced, which avoids the possibility of infinite recursion when dealing with an alias like the following:
alias ls = ‘ls -F’
How to Create aliases on Linux
Creating aliases is a relatively easy and quick process. You can create two types of aliases – temporary and permanent. Let’s review both types.
Creating temporary nicknames
What you need to do is type the word alias and use the name you want to use to execute a command followed by the “=” sign and cite the command you want to create.
The syntax is as follows:
$ alias shortName = “your custom command here”
Here is a real example:
$ alias wr = “cd / var / www / html”
You can then use the “wr” shortcut to go to the webroot directory. The problem with this alias is that it will only be available to your current terminal session.
If you open a new terminal session, the alias will no longer be available. If you want to save your aliases between sessions, you will need a permanent alias.
Creating Permanent Aliases
To keep aliases between sessions, you can save them in the user’s shell configuration profile file. This can be:
Bash – ~ / .bashrcZSH – ~ / .zshrcFish – ~ / .config / fish / config.fish
The syntax you should use is pretty much the same as creating a temporary alias. The only difference comes from the fact that you will be saving it to a file this time. For example, in bash, you can open the .bashrc file with your favorite editor like this:
$vim ~ / .bashrc
Find a location in the file where you want to keep the nicknames. For example, you can add them to final of the file. For organizational purposes, you can leave a comment before your aliases something like this:
#My custom aliassalias home= ”Ssh -i ~ / .ssh / mykep.pem tecmint@192.168.0.100” alias ll = ”ls -alF”
Save the file. The file will be automatically uploaded on your next session. If you want to use the newly defined alias in the current session, issue the following command:
$ source ~ / .bashrc
To remove an alias added via the command line, you can disable the alias using the unalias command.
$ unalias alias_name $ unalias -a [remove all alias]
Final note
I hope you like the guide How to set up and use aliases in Linux shell commands. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.