• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

Examples of creating command alias in different shells

by admin

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]
How to use shell aliases in Linux

Filed Under: DevOps, Linux, Shell Scripting

Some more articles you might also be interested in …

  1. CentOS / RHEL : Configure yum automatic updates with yum-cron service
  2. jpegtran for image optimization
  3. pwd Command Examples in Linux
  4. named Command Examples in Linux
  5. zcat Command Examples in Linux
  6. isoinfo: command not found
  7. How to set nproc (Hard and Soft) Values in CentOS / RHEL 5,6,7
  8. more: command not found
  9. iftop: command not found
  10. insmod: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • powertop Command Examples in Linux
  • powertop: command not found
  • powerstat: command not found
  • powerstat Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright