Skip to content

Linux Foundations / Find your way around Linux ยท Step 29 of 63

Lesson: aliasSwitch
โ† Linux Foundations

Shell and files

alias

Create shortcuts for long commands with alias and make them permanent.

An alias is a short name that the shell replaces with a longer command. It saves typing and lets you set options you always want.

Creating an alias

$ alias ll='ls -lh'
$ ll
total 4.0K
-rw-r--r-- 1 pete pete 12 Nov 29 10:00 a.txt

Write no spaces around =. Quote the value so the whole command stays together. Anything you type after the alias is added to the end, so ll /etc runs ls -lh /etc.

Seeing your aliases

Run alias with no arguments to list them all, or give a name to see one:

$ alias ll
alias ll='ls -lh'

type ll also shows that it is an alias. Many distributions define a few for you.

Removing and skipping

unalias ll removes an alias from the current shell.

To run the real command once, without the alias, put a backslash in front or use command:

$ \ls
$ command ls

Making it permanent

An alias typed at the prompt lasts only until that shell exits. To keep it, add the line to a startup file. For bash that is usually ~/.bashrc:

$ echo "alias ll='ls -lh'" >> ~/.bashrc
$ source ~/.bashrc

Use two >> so you append. A single > would erase the file. source reads it again in the current shell, and new interactive shells normally read it automatically. Some systems, including macOS, use other files, and zsh uses ~/.zshrc.

Limits

Aliases cannot place arguments in the middle of a command. If you need that, write a shell function. Bash also does not expand aliases in scripts by default, so do not rely on them there.

Avoid naming an alias after an important command with surprising behaviour. You may forget it exists and be caught out on another machine.

Try it

  1. Create alias la='ls -A' and use it.
  2. List all your aliases.
  3. Remove it with unalias la.
  4. Add an alias you like to ~/.bashrc and open a new terminal.

Check your understanding

alias

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.