Examples of creating command alias in different shells

Alias for commands lets you define your own short easy to remember command shortcuts. Below are some examples of defining command aliases permanently into the different shells like bash, ksh and sh. You can also define aliases on command line, but they will not persist after you change the shell or logout of the shell.

Bash Shell

To create a command alias for the bash shell:
1. Login as user.

2. Add the following lines to .bashrc in home directory of the user :

$ vi ~/.bashrc
alias [alias_name]='[command]'
export [alias_name]

For example:

$ vi ~/.bashrc
alias l='ls -lrt'
export l

3. No logout of the shell and login back again.

4. Run the command used in alias.

$ [alias_name]

In our example, we will use :

$ l
NOTE: Create .bashrc file if it’s not already there.

ksh Shell

To create a command alias for the ksh shell:
1. Login as user.

2. Add the following lines to .kshrc in home directory of :

$ vi ~/.kshrc
alias [alias_name]='[command]'
export [alias_name]

For example:

vi ~/.bashrc
alias c='clear'
export c

3. Exit out of the shell and login back again.

4. Run the command used in alias:

$ [alias_name]
NOTE: Create .kshrc file if it’s not already there.

sh Shell

To create a command alias for the /bin/sh shell:
1. Login as user.

2. Add the following lines to .profile in home directory of :

$ vim ~/.profile
alias [alias_name]='[command]'
export [alias_name]

For example:

$ vi ~/.profile
alias d='cd /some/log/directory'
export d

3. Logout of the shell and login back again.

4. Run the command used in alias to verify if it works as desired.

$ [alias_name]

Defining command alias temporarily on command line

You can also define the alias temporarily for the logged in shell using the command below. Please note, the alias will stop working if you change the shell or logout from the shell and login back. The below commands work for all the shells.

# alias [alias_name]='[command]'
# export [alias_name]
Related Post