Tuesday, March 19, 2024
HomeA-Z CommandsUsing alias Command in Linux To Improve Your Efficiency

Using alias Command in Linux To Improve Your Efficiency

The alias command is used in Linux to execute the commands with a reference value of a particular syntax. In a way, the alias works as a variable for the command. You can assign an alias for a particular command, and later instead of writing the actual command, you can just use your given alias value while executing. This way, you can make your terminal command less complicated and easy to remember.

Using the alias in command can make your experience smoother; it helps us avoid spelling mistakes and remember the command. If you’re a database engineer, the term alias might be quite familiar. In relational DB engines, programmers often use aliases to make the database more understandable. In Linux, using the alias command on the shell is genuinely cool and convenient; also, it gives you a professional vibe.

Syntaxes of Alias Command in Linux


The alias command has only a few syntaxes that you need to remember. Actually, most people use the alias command to avoid remembering the command syntax, so the alias command is designed in a manner so that you don’t need to remember much of alias syntaxes. Here, we will see a few syntaxes and their use-cases.

Here is the format of the Alias command on Linux

alias [option] [name]='[value]'
  • Alias: The alias syntax initializes the alias processes on Linux.
  • [option]: This option lets the user see all the ongoing aliases on the Linux system.
  • [name]: In the alias command, the name refers to the actual command value. The name is a string value; users can write names as they wish to use.
  • [value]: The value works as a reference of the command. You can use arguments, paths, and symbolic links in the command to refer to the value.

Examples of Alias Commands on Linux


An alias command works as a shortcut to the actual command. You can use both temporary and permanent aliases on your Linux machine. The syntaxes and the command format are easy to understand; you can learn them with no extra effort.

Through an alias command, you can replace a longer command with a short command, assign letters and alphabets to different commands, replace the string with an alias, and many more. The alias comes pre-installed with the OS in most major Linux machines and servers.

In Linux, there are two types of aliases; they are permanent and temporary. The temporary alias refers to a command or value in the ongoing terminal shell; the temporary alias ends when you close the current terminal session.

The permanent alias is used inside the shell script and bash files. If you need to permanently modify or use an alias, you will need to edit your system files on the etc or other root directories. This post will see how to use the examples of the alias command on a Linux system.

1. Start with the Alias Command


Getting started with the alias command is hassle-free. You can just simply type alias on the shell to check if the command works on your system or not. The alias command will return with the available aliases and paths on the system.

$ alias

2. Use Alais to Print a Reference Value in Shell


It is considered as executing a command through another command. This command works as an argument in Linux.

$ date
$ echo The date today is `date`

print date by alias by Linux

3. Use a Variable to Hold a Command


You can use the alias to store a value for the command; later, you can use that alias for calling the data on the command. For example, the alphabet d holds the date parameter in the below-mentioned command.

put as d=`date`
$ echo $d

4. Double Quotation Marks


In Linux, you can use the quotation marks in the command to call an alias. We can use both single and double quotations on the command. They both have different meanings and use-case with an alias. The double quotations are defined to call or expand the variable value.

For instance, executing the PWD command on the shell shows the current directory path. If we set an alias on the directory with the double quotations, it will be executed and expanded with the actual value.

$ echo $PWD
/home/ubuntupit
$ alias dirA="echo Working directory is $PWD"
$ alias dirA
alias dirA='echo Working directory is /home/ubuntupit’

double quotation marks in aliases

5. Single Quotation Marks


The single quotation marks are only used to call the variable name, and it does not print the value of the variable. The below-mentioned example can clarify the notion of using the single quotations with an alias command on Linux.

Here, you can see that it only calls the variable PWD and doesn’t expand the value that we assigned earlier.

$ alias dirB='echo Working directory is $PWD'
$ alias dirB
alias dirB='echo Working directory is $PWD'

single quotation marks

6. Prevent the Shell from Invoking an Alias


The terminal shell generally looks for the aliases when executing a shell command. It can automatically detect quoted, unquoted, relative, or other aliases on the system. If you want to prevent your Linux terminal from initializing an alias on the system, you can use a backslash (/) with the command. It conventionally works as commenting on a line in the script with the hash (#) value.

For instance, the below-mentioned alias command will allow us to refer to the alphabet r to call the repeat command.

$ alias r='fc -s'

If we often execute the below-mentioned ls -ltr command, the shell will allow us to use the alias l to call the ls -ltr command.

$ alias l='ls -ltr'
$ l

alias l and r command for repeat tasks on Linux

If we set another alias as ls for the value ‘ls -F’ and then we use one more alias as ll=’ls -l’, we can see that in the second alias, the ls itself is an alias in the first command.

$ alias ls='ls -F'
$ alias ll='ls -l'
$ \ll

use backslash on alias command on Linux

So in this way, if we keep continuing to assign one alias for another command, even one alias for two different commands, chances are higher that we’ll get an error in a complicated command or we won’t get the expected result.

So, to avoid this issue, we can add a backslash (\) with the command that we want to execute in the normal way without any aliases.

7. Run Alias in For Command


In Linux, we can use the alias command to ensure that we are not making errors or typing mistakes while writing the commands. In this way, we can use an alias to replace a complicated command.

For instance, we can assign the zap for the rm command, and if we apply the zap command as an alias, it will perform the removal functions. We are using the -i flag in the command, which will ask us before the deletion.

The below-mentioned alias command will remove files that start with the alphabet f.

$ alias zap='rm -i'
$ zap f*

use zap for remove with alias command on Linux

You can see here the command asked for the approval to remove; when I executed with a ‘y’, only then did it initiate the removal process.

8. Show All Aliases


To see all the active aliases on your Linux system, you can simply type alias and hit the enter button in the terminal shell.

$ alias

show all alias

9. Remove an Aliases


To remove an alias command from your Linux machine, you can just simply unalias the value. For instance, the below-mentioned command will allow us to remove the alias for the term ubuntupit that we set earlier.

$ unalias [-a] name(s)
$ unalias ubuntupit

10. Set the Alias for the Directory


The alias command also allows the users to set an alias for a directory. For instance, the below-mentioned command will let us set the alias ubuntupit for the home directory.

$ alias ubuntupit='cd /home/ubuntupit/'
$ ubuntupit

11. Check If The Command is an Alias or An Actual Command


If you need to check whether the command you’re running is an alias or an actual command, you can run the which command to check the source of that value. For instance, the below-mentioned which command prints the actual path, ensuring that it’s an actual command.

$ which date

Check if your command is an alias or an actual command

If we have been getting an output like the below result, it would be an alias.

$ which ll
alias ll='ls -alF'
/usr/bin/ls

12. Checking Your OS Release Via Alias Command On Linux


The use of the alias command on Linux is versatile. You can even set an alias to check your OS version and details.

$ alias rel='lsb_release -a'
$ rel

Checking your OS release

13. Check the Number of Aliases


If you often use the alias command on your Linux machine, you might be conscious of how many aliases you’re using and what they are. To view the total number of aliases currently active on your Linux system.

$ alias | wc -l

Check number of aliases

14. Edit Bash For Alias


This one is very helpful if you want to make an alias command permanent on your Linux system. For instance, if we want to make an alias permanent on Linux, we can just edit the /.bashrc file and put the alias we want to use.

The below-mentioned command will open the /.bashrc file on the nano text editor. After you open the file, please scroll down at the bottom and but the alias and save the script.

sudo nano ~/.bashrc
alias c='clear'

edit alias bash on Linux

15. Alias Help on Linux


Last but not least, to get more help and manual regarding the alias command on Linux, you can

$ alias --help

alias --help

Insights!


Using the alias command is fun if you know what you’re doing. You can create aliases as many as you need to make your commands easy and easy to remember. In the entire post, I’ve described the syntaxes, notions, and a few most used examples of the alias command on Linux.

If you find this article has been useful for you, please do not forget to share this post with your friends and the Linux community. We also encourage you to write down your opinions in the comment section regarding this article.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

You May Like It!

Trending Now