Bash alias example | Linux

Shell Scripting

We will create a command of our own using the alias command. But before we start, we need to reveal a small command-line trick. It’s possible to put more than one command on a line by separating each command with a semicolon character. It works like this:

command1; command2; command3

Here’s the example we will use:

[[email protected] ~]$ cd /usr; ls; cd –

bin games kerberos lib64 local share tmp etc include lib libexec sbin src


[[email protected] ~]$

As we can see, we have combined three commands on one line. First we change directory to /usr, then we list the directory, and finally we return to the original directory (by using cd -) so we end up where we started. Now let’s turn this sequence into a new command using alias. The first thing we have to do is dream up a name for our new command. Let’s try test. Before we do that, it would be a good idea to find out if the name test is already being used. To find out, we can use the type command again:

[[email protected] ~]$ type test

test is a shell builtin

Oops! The name test is already taken. Let’s try foo:

[[email protected] ~]$ type foo

bash: type: foo: not found

Great! foo is not taken. So let’s create our alias:

[[email protected] ~]$ alias foo=’cd /usr; ls; cd -‘

Notice the structure of this command:

alias name=’string

After the command alias we give the alias a name followed immediately (no whitespace allowed) by an equal sign, which is followed immediately by a quoted string containing the meaning to be assigned to the name. After we define our alias, it can be used anywhere the shell would expect a command.

Let’s try it:

[[email protected] ~]$ foo

bin games kerberos lib64 local share tmp etc include lib libexec sbin src


[[email protected] ~]$

We can also use the type command again to see our alias:

[[email protected] ~]$ type foo

foo is aliased to `cd /usr; ls ; cd -‘

To remove an alias, the unalias command is used, like so:

[[email protected] ~]$ unalias foo

[[email protected] ~]$ type foo

bash: type: foo: not found

While we purposely avoided naming our alias with an existing command name, it is sometimes desirable to do so. This is often done to apply a commonly desired option to each invocation of a common command. For instance, we saw earlier how the ls command is often aliased to add color support:

[[email protected] ~]$ type ls

ls is aliased to `ls –color=tty’

To see all the aliases defined in the environment, use the alias command without arguments. Here are some of the aliases defined by default on a Fedora system. Try to figure out what they all do:

[[email protected] ~]$ alias

alias l.=’ls -d .* –color=tty’

alias ll=’ls -l –color=tty’

alias ls=’ls –color=tty’

There is one tiny problem with defining aliases on the command line. They vanish when your shell session ends.

In case of any ┬ęCopyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.