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.txtWrite 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 lsMaking 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 ~/.bashrcUse 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
- Create
alias la='ls -A'and use it. - List all your aliases.
- Remove it with
unalias la. - Add an alias you like to
~/.bashrcand 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.