You are on a server with no browser, and you need the flag that sorts ls by size. Or a command works when you type it but not inside a script. The answer to both is on the box itself: it can tell you what a word means, where the program lives, and what every flag does - for the exact version installed.
What you need to know already: 1.7 Driving the shell (pipes, $?, grep).
The box documents itself
You will not remember every flag. Nobody does. What you need is the reflex of asking the machine in front of you instead of the internet's average of every version ever released.
Variables, briefly
The shell keeps named values called variables. You read one by putting $ in front of its name, and the shell swaps in the value before running the command:
$ echo $HOME
/home/learner
$ echo $USER
learner
Some variables are handed to every program the shell starts - these are environment variables (HOME, USER, PATH...). $? from lesson 1.7 is a special one the shell sets itself.
What is this word? type
When you type a command name, bash can find it in four kinds of place. It checks them in this order:
- an alias - a shortcut name that expands to a longer command (
llmeansls -alF); - a function - a small named set of commands defined inside the shell;
- a builtin - a command built into bash itself (
cd,echo); - a file - a program stored on disk, found by searching the PATH (below).
type tells you which one wins:
$ type cd ls ll grep
cd is a shell builtin
ls is aliased to `ls --color=auto'
ll is aliased to `ls -alF'
grep is aliased to `grep --color=auto'
$ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
-a (all) lists every match, not just the winner. ls shows up twice as a file because /bin is a symlink to /usr/bin on a modern Ubuntu, and both are on the PATH - same file, two routes to it.
This matters more than it looks:
cdmust be a builtin. A separate program could only change its own directory, then exit - your shell would not move.lsin your terminal is not thelsa script runs. Aliases come from~/.bashrcand only exist in interactive shells (the ones you type into), so colours andllvanish inside scripts and undersudo.
Where is it? which and command -v
$ which ls bash
/usr/bin/ls
/usr/bin/bash
$ which tree
$ echo $?
1
$ command -v jq || echo "not installed"
not installed
which searches the PATH and prints the first match (nothing, and exit 1, if there is none). command -v is bash's builtin version: it also knows about aliases and functions, and it is the reliable way for a script to ask "is this installed?".
$PATH is just a list of directories, separated by colons, that the shell searches in order when you type a command name:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
A program in the current directory is not found unless you say ./prog. That is deliberate: otherwise anyone who could put a file called ls in /tmp would take over your account the moment you cd /tmp and typed ls.
Command not found
$ tree
Command 'tree' not found, but can be installed with:
sudo apt install tree
Ubuntu has a helper that knows which package (an installable bundle of software - lesson 1.11) provides which command, and tells you. The exit status is
- On a stripped-down system without that helper you get the bare
bash: tree: command not found.
--help: the fast answer
Almost every program prints a summary of its flags with --help:
$ ls --help | head -5 the usage line and the first options
$ ls --help | grep -- -S
-S Sort by file size, largest first.
Piping into grep narrows a long help text to the line you care about. The -- in grep -- -S means "end of options": without it grep would treat -S as one of its own flags. Remember it whenever you search for something starting with a dash.
man: the full answer
man opens a command's manual page:
man ls
man 5 crontab section 5: the format of the crontab file (a schedule of
recurring jobs), not the crontab command
man -k journal search every page's one-line description (same as apropos)
(The journal is where Linux keeps its system logs; chapter 2 reads it.)
A man page opens in the pager - a program (less) that shows long text one screen at a time. The keys:
Space / b page down / page up
/pattern search forward, n = next match, N = previous
g / G top / bottom
q quit
Manual pages are grouped in numbered sections: 1 commands, 5 file formats, 8 admin commands. Every page has the same layout: NAME, SYNOPSIS, DESCRIPTION, OPTIONS, EXAMPLES, SEE ALSO. Read SYNOPSIS first - [ ] means optional, ... means "can repeat".
help: for builtins
Builtins have no man page of their own (man cd drops you in the giant bash page). Use bash's help:
help cd
help test
help set
In this simulator, plain help also lists the game commands.
The habit
Before copying a command from anywhere: type it, --help it, and look up the one flag you do not recognise. It takes ten seconds, and it is how you stop running things you do not understand as root.
What you can now do
- Tell whether a word is an alias, builtin, function or program, and where it lives.
- Find a flag with
--help | grep, or a whole page withman/man -k. - Read
$PATHand explain why./progneeds the./.