Introduction

What Aliases Are

Here is a short introduction to aliases in case you have not idea what they are:

An alias is simply a different name for a specific command line command. You can call an alias like any other program and the shell will then replace the alias with the command it represents.

If you find yourself typing”aptitude” a lot you could, for example, define an alias “a” for this command instead. Now, instead of typing

aptitude install <package>

you just need to type

a install <package>

Bash will recognize that the command a is really just an alias that stands for “aptitude” and therefore replace a with “aptitude”. You could make your like even easier by assigning an alias “ai” to the command “aptitude install”. Then, when typing

ai <package>

bash will notice that the command “ai” is really just an alias for “aptitude install” and replace “ai” accordingly. Therefore, the command that bash will execute is:

aptitude install <package>

Permanently Storing Your Aliases

It is important to note than running the commands below will only change the alias definitions for the current shell. The changes will not appear in other shells and they will be lost when closing the shell (and logout/shutdown/reboot). This basically means that the changes are only temporary.

Therefore, if you want to permanently define new aliases, you can create new file named

.bash_aliases

in your homefolder (the file will most likely not exist already) and put all you alias definitions in there. Alternatively you can add the definitions to the file

.bashrc

in your home folder, but it’s cleaner to have a separate file for the aliases and the .bashrc should already check for a file named .bash_aliases, so you only need to create the .bash_aliases and the file will automatically loaded.

It is important to note that the .bashrc and therefore the .bash_aliases will only be read when the shell is created. Therefore, you won’t see the changes to .bash_aliases immediately. You need to either create a new shell after saving your modified .bash_aliases or (re)load the .bash_aliases file like this:

. ~/.bash_aliases

You can, of course, create an an alias for this command in your .bash_aliases file…

 

List All Aliases

To get a list of all aliases which are currently defined, all you need to do is to run:

alias

This will list all defined aliases, one per line, like this:

alias la='ls -la'
alias ls='ls --color=auto'
alias lx='ls -lAh --group-directories-first'

 

List A Specific Alias

To only get the value of a specific alias, you can type:

alias <name>

You can also use this command to display several aliases:

alias <name1> <name2> ...

Example 1

alias la

will display

alias la='ls -la'

Example 2

alias ls la

will display:

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

Create A New Alias / Change An Existing Alias

Creating a new alias or changing the value of an existing alias can be done in the same way:

alias <name>=<value>

For example, creating an alias “www” to avoid typing “cd /var/www” all the time, can be done like this:

alias www='cd /var/www'

 

Combining Aliases

When defining an alias it is possible to use another alias. Let me show you two examples of how this might be put to use:

Supplying (additional) options

The only predefined alias on Debian is

ls='ls --color=auto

By defining an alias la like this:

alias la='ls -la'

You already used an alias for defining the new alias. When typing

la

bash will now recognize that “la” is an alias and replace it with its value:

ls -la

But now, again, bash will recognize that “ls” is an alias as well and replace this will its value “ls –color=auto”, which results in the command:

ls --color=auto -la

(bash will not recognize ls an alias again, so this is where it stops)

In this example the alias “la” used the already defined alias “ls” (which adds coloring to the command ls) to add the additional options “-l” and “-a” to it. This can be used for various purposes. If you want to define various aliases for the ls command, for example, it might be a good idea to create an alias that already contains the common options (like coloring) you want to use for all the other aliases. This will shorten the alias values, which makes them easier to manage. Instead of defining the following aliases:

alias la='ls -la --color=auto'
alias lh='ls -lh --color=auto'
alias lx='ls -lAh --color=auto --group-directories-first'

you could define them like this:

alias la='ls -a'
alias lh='ls -h'
alias ls='ls -l --color=auto'
alias lx='lh -A --group-directories-first'

In this example the alias ls already carried the “-l” and “–color=auto” option, therefore avoiding the others to specify them as well. The definition of the lx alias uses the lh alias, which additionally carries the “-h” option. You should be careful when using this approach, because creating two many connections between aliases can make management more complicated instead of simplifying it and also create some unwanted side effects.

 

 Supplying (additional) arguments

Similar to applying additional command line options, you can supply additional arguments to

 

Delete An Alias

To delete an alias you need to use the unalias command:

unalias <name>

It is also possible to delete several aliases at once:

unalias <name1> <name2> ...

Example

To delete the www alias that we just created, type:

unalias www

 

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>