← Linux and the Command Line Lesson 10 of 51 · 5 XP

alias — your own short commands

Theory

Machine translation, not yet reviewed by a native speaker.

alias creates a short name for a long command:

alias ll='ls -la'
alias gs='git status'
alias ..='cd ..'
alias please='sudo'

ll                     # runs ls -la
alias                  # show all the aliases
unalias ll             # delete one

An alias lives only in the current terminal window. To have it always, add the line to the shell's settings file:

  • bash — ~/.bashrc;
  • zsh (the default in Kali) — ~/.zshrc.
echo "alias ll='ls -la'" >> ~/.bashrc
source ~/.bashrc       # reread the settings without restarting the terminal

Useful aliases for safety:

alias rm='rm -i'       # ask before deleting
alias cp='cp -i'
alias mv='mv -i'

Limitations:

  • aliases usually don't work in scripts: bash expands them only in interactive mode. In scripts, use functions (the "Scripts" module);
  • an alias can't take arguments "in the middle". If you need mkcd folder (create a folder and go into it), you need a function too:
mkcd() { mkdir -p "$1" && cd "$1"; }

To find out what actually runs under a name: type ll → ll is aliased to 'ls -la'.

Try it yourself: Programmers' Room → Linux · sh.

Sign up for free to mark finished lessons and track your progress.