CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Environment & Shell / alias

alias

Create shortcuts for commands you type all the time

Beginnershellproductivity

alias defines a short name that the shell replaces with longer text before running a command, like k for kubectl or ll for ls -alF. Aliases only exist in interactive shells, are expanded as text, and are easy to bypass when you need the real command.

Syntaxalias [NAME[='VALUE']]... | unalias [-a] NAME...

Watch it run

You type kubectl and ls -lah hundreds of times a day. Make shortcuts.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
alias

Ubuntu's default ~/.bashrc already defines these. That's why ls is coloured: ls itself is an alias.

Anatomy of a command

$
commandalias

A shell builtin. With no arguments it lists every alias.

How it works

Aliases are pure text substitution done by the shell's parser. When bash reads a command line in an interactive shell, it checks whether the first word of each simple command is an alias. If so, it replaces the word with the alias text and checks the new first word again, so aliases can chain (ll -> ls -alF -> ls --color=auto -alF) but a word isn't expanded twice in a row. If the alias value ends in a space, the next word is checked too, which is why alias sudo='sudo ' lets aliases work after sudo. Expansion happens when a line is read, before any execution, so an alias defined on a line can't be used later on that same line. In non-interactive shells (scripts, cron, sh -c) the expand_aliases option is off, so aliases don't exist there. A backslash or quotes on the word (\ls, 'ls'), or command ls, skip the alias. Aliases can't take arguments in the middle; use a function for that.

Options you'll actually use

aliasList all aliases
alias NAMEShow one alias
alias NAME='VALUE'Define or overwrite
unalias NAME / unalias -aRemove one / remove all
\NAME or command NAMERun the real command, skipping the alias
type NAMEShow whether NAME is an alias and what it expands to
~/.bash_aliasesWhere Ubuntu's .bashrc loads personal aliases from

Common mistakes

Using aliases (k, ll, tf) inside scripts or cron jobs

Non-interactive shells don't expand aliases, so the script fails with 'command not found'.

Write kubectl / ls -lah / terraform in full in scripts
alias rm='rm -rf'

Every rm becomes recursive and forced; one typo deletes a tree without asking.

alias rm='rm -I' (prompt once for many files), or no rm alias at all
alias gp="git push origin $(git branch --show-current)"

Double quotes run the $(...) once, when the alias is defined, so it always pushes the branch you were on then.

alias gp='git push origin "$(git branch --show-current)"' # single quotes: evaluated at use
Expecting 'sudo ll' to work

Only the first word is checked for aliases; ll after sudo isn't expanded.

alias sudo='sudo ' # trailing space makes the next word alias-expanded too

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List your current aliases. The sandbox shell is zsh; then start bash and compare.

  2. 2

    In bash, create an alias logs that shows the last 20 lines of ~/logs/app.log, use it, then check it with type.

  3. 3

    Make the alias permanent for bash and verify it in a new bash shell.

  4. 4

    ll is an alias in the sandbox. Run the real ls without the alias's options.

Related commands

typeSee exactly what the shell will run for a namesourceRun a file's commands in the current shellhistorySearch, reuse and clean your command historywhichFind which executable runs for a command name
PreviousexportNexthistory
Linux / Environment & Shell / alias

alias

Create shortcuts for commands you type all the time

Beginnershellproductivity

alias defines a short name that the shell replaces with longer text before running a command, like k for kubectl or ll for ls -alF. Aliases only exist in interactive shells, are expanded as text, and are easy to bypass when you need the real command.

Syntaxalias [NAME[='VALUE']]... | unalias [-a] NAME...

Watch it run

You type kubectl and ls -lah hundreds of times a day. Make shortcuts.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
alias

Ubuntu's default ~/.bashrc already defines these. That's why ls is coloured: ls itself is an alias.

Anatomy of a command

$
commandalias

A shell builtin. With no arguments it lists every alias.

How it works

Aliases are pure text substitution done by the shell's parser. When bash reads a command line in an interactive shell, it checks whether the first word of each simple command is an alias. If so, it replaces the word with the alias text and checks the new first word again, so aliases can chain (ll -> ls -alF -> ls --color=auto -alF) but a word isn't expanded twice in a row. If the alias value ends in a space, the next word is checked too, which is why alias sudo='sudo ' lets aliases work after sudo. Expansion happens when a line is read, before any execution, so an alias defined on a line can't be used later on that same line. In non-interactive shells (scripts, cron, sh -c) the expand_aliases option is off, so aliases don't exist there. A backslash or quotes on the word (\ls, 'ls'), or command ls, skip the alias. Aliases can't take arguments in the middle; use a function for that.

Options you'll actually use

aliasList all aliases
alias NAMEShow one alias
alias NAME='VALUE'Define or overwrite
unalias NAME / unalias -aRemove one / remove all
\NAME or command NAMERun the real command, skipping the alias
type NAMEShow whether NAME is an alias and what it expands to
~/.bash_aliasesWhere Ubuntu's .bashrc loads personal aliases from

Common mistakes

Using aliases (k, ll, tf) inside scripts or cron jobs

Non-interactive shells don't expand aliases, so the script fails with 'command not found'.

Write kubectl / ls -lah / terraform in full in scripts
alias rm='rm -rf'

Every rm becomes recursive and forced; one typo deletes a tree without asking.

alias rm='rm -I' (prompt once for many files), or no rm alias at all
alias gp="git push origin $(git branch --show-current)"

Double quotes run the $(...) once, when the alias is defined, so it always pushes the branch you were on then.

alias gp='git push origin "$(git branch --show-current)"' # single quotes: evaluated at use
Expecting 'sudo ll' to work

Only the first word is checked for aliases; ll after sudo isn't expanded.

alias sudo='sudo ' # trailing space makes the next word alias-expanded too

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    List your current aliases. The sandbox shell is zsh; then start bash and compare.

  2. 2

    In bash, create an alias logs that shows the last 20 lines of ~/logs/app.log, use it, then check it with type.

  3. 3

    Make the alias permanent for bash and verify it in a new bash shell.

  4. 4

    ll is an alias in the sandbox. Run the real ls without the alias's options.

Related commands

typeSee exactly what the shell will run for a namesourceRun a file's commands in the current shellhistorySearch, reuse and clean your command historywhichFind which executable runs for a command name
PreviousexportNexthistory