Working with Commands
3 min read
Core idea
When you type a word at the prompt and press Enter, the shell has to decide what to do with it. That word can resolve to one of four different things: an external executable program (a file in your PATH), a shell builtin (code compiled into bash itself, like cd), a shell function (a chunk of script defined in your environment), or an alias (a shortcut you defined yourself). The first time you wonder "is ls actually colourised, or is something else going on?", the answer is almost always an alias hiding the real command.
This topic teaches you to be the detective. With type, which, help, man, info, apropos, and alias you can ask the shell exactly what it would run, where the underlying file lives, and what the official documentation says. You can then build your own commands with alias, taking the first tiny step into shell programming.
Why it matters
Mental model
What kind of command is this?
Bash resolves names in a deterministic order. The first match wins.
The documentation tree
| Tool | What it answers |
|---|---|
| type <cmd> | What kind of thing is this (alias / function / builtin / file)? |
| which <cmd> | Where on disk is the executable? (only works for actual files) |
| help <builtin> | Quick help on a shell builtin (cd, type, alias, ...). |
| <cmd> --help | Most external programs print a usage summary. |
| man <cmd> | The full manual page — the canonical reference. |
| apropos <word> or man -k <word> | Search every man page's one-line description. |
| whatis <cmd> | The one-line summary of the man page. |
| info <cmd> | GNU's hyperlinked alternative to man (heavier, with examples). |
Reading man-page syntax
When a man page describes a command's usage you will see notation like:
cd [-L|[-P [-e]]] [dir]
Square brackets are optional. A vertical bar separates mutually exclusive choices. So this line says: cd may optionally take either -L or -P (and if -P, optionally -e too), followed optionally by a directory argument. Once you can read this notation, every command's docs become approachable.
Practical application
Build your own commands with alias
-
Combine commands with
;. A semicolon separates commands on a single line, e.g.cd /tmp; ls; cd -. -
Pick a name that's free. Check it:
type myname. If you getcommand not found, the name is yours. -
Define the alias.
alias myname='cd /tmp; ls; cd -'. No spaces around=. Quote the value to keep it together. -
Use it. Just type
myname. The shell expands the alias before running. -
Make it permanent. Add the same
aliasline to~/.bashrc. New shells will pick it up automatically. -
Remove it.
unalias mynamedeletes the alias from the current shell.
Useful aliases to consider
alias ll='ls -lhF' # long listing, human sizes, classifier glyphs
alias la='ls -lAhF' # ll plus hidden files
alias ..='cd ..' # one level up
alias ...='cd ../..' # two levels up
alias grep='grep --color=auto' # always colourise
alias rm='rm -i' # ask before deleting (training wheels)
alias df='df -h' # human-readable sizes
alias free='free -h' # likewise
To see everything currently aliased in your shell, run alias with no arguments.
Example
You sit down at a colleague's terminal and notice their ls output is colourised in a way yours is not. You want to know why. The investigation is two commands:
$ type ls
ls is aliased to `ls --color=auto'
$ alias | grep '^alias ls'
alias ls='ls --color=auto'
So ls is an alias to itself plus --color=auto. The shell expands ls foo into ls --color=auto foo before exec'ing the real /usr/bin/ls. You suspect their system has more interesting aliases; you list them all:
$ alias
alias ..='cd ..'
alias g='git'
alias gst='git status'
alias k='kubectl'
alias ll='ls -lhF'
alias ls='ls --color=auto'
You like the g shortcut. To copy it to your own machine permanently you add a single line to your ~/.bashrc:
echo "alias g='git'" >> ~/.bashrc
source ~/.bashrc
The next shell — and every shell after — will have g pre-defined. You have just productised somebody else's habit in three lines. This is the on-ramp to shell scripting.
Related lessons
Related concepts
- Shelllinked concept
- Command Linelinked concept
- Bash Scriptinglinked concept