Unix Bash Alias Tutorial – Handle Alias Command Like Jennifer Garner

While most of us cannot kick someone ass like Jennifer Garner in Alias, we can at least try to use the Linux alias command effectively.

An alias command is simple string substitution of one text for another, when it is used as the first word of a simple command.

In this article let us review how to set / unset bash aliases permanently and temporarily. Let us also review some useful bash alias examples that you can use in your bash startup files.
This article is part of the on-going bash tutorial series. Refer to our earlier tutorial about bash introduction and bash exit status.

How to Set an Alias

Aliases can be defined on the command line, in .bash_profile, or in .bashrc, using the following syntax:

$ alias name='unix command with options'

alias – is a shell built-in

name – any user-defined simple name for the alias.

command – any unix command, which could be with the options.

This means that name is an alias for command. Whenever name is typed as a command, bash will substitute the corresponding command along with the options in its place.

Note: There are no spaces on either side of the equal sign. Quotes around command are necessary if the string being aliased consists of more than one word.

Executing this command in command line makes this as temporarily alias. That is, this alias is available until you exit the shell. Storing the alias in bash startup files makes it as permanent alias.

The following aliases might be useful. You can set these aliases in ~/.bashrc file.

Most Common Alias Examples

Following aliases are ready for you to use straight away. What is your favorite alias?

How to view all the aliases

To view a particular alias, enter the command the format “alias aliasname” as shown below.

$ alias dus
alias dus='df -h'

How to temporarily stop using aliases

When you want to call the command instead of the alias, then you have to escape it and call.

$ \aliasname

For Example, alias cp=”cp -iv”, will ask you confirmation if you are about to overwrite a file. This can be annoying when you are copying lot of files that you already know you are going to overwrite. Probably you might want to temporarily use the regular cp command instead of the cp-alias.

So, if an alias cp exists, but you want to use the cp-command instead, escape the alias temporarily as shown below:

\cp * /backup/files/

How to remove an alias

unalias is a shell built-in to remove an alias. To remove a particular alias:

$ unalias hcl
where unalias is a shell built-in.
mnt is an alias name.
$ hcl
-bash: hcl: command not found

How to remove all aliases

unalias with -a option, removes all the aliases.

$ unalias -a
$ alias

NOTE: Shell functions are faster. Aliases are looked up after functions and thus resolving is slower. While aliases are easier to understand, shell functions are preferred over aliases for almost every purpose. You should be very careful replacing a standard command with an alias or a function.

The aliases shown in this article are available in this sample bash aliases files, which you can copy/paste and use it on your bash startup file.

Aliases are very useful things, but I hope that you will find bash functions at least as interesting and even more useful which we will discuss in our next bash article.

@Shantanu – after making the changes in .bashrc, if you want to use the alias immediately you can source the file.
source ~/.bashrc
or
. ~/bashrc
This will make the changes available to the current shell itself.

Thanks Mr. TGS. I have a question : how to include subcommands in alias ? I need to do :
alias myAlias=”command options $(subcommand options)”
but when I type “alias” in the bash, I see
alias myAlias=”command options “.
Seems the whole subcommand has been stripped out. Is there a solution ? Or should I create a function instead ? TIA

About The Geek Stuff

My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about Ramesh Natarajan and the blog.

Contact Us

Email Me :
Use this Contact Form to get in touch me with your comments, questions or suggestions about this site. You can also simply drop me a line to say hello!.