Table of Contents
In the shell, most things you do follow a predictable pattern called command structure. Understanding this pattern makes commands easier to read, remember, and combine.
Basic command pattern
A simple command usually looks like:
$$\text{command} \; [\text{options}] \; [\text{arguments}]$$
command: the program or built‑in you run (e.g.,ls,cp,echo)options(also called flags or switches): tweak how the command behavesarguments: what the command should act on (files, directories, text, etc.)
Example:
ls -l /homels→ command-l→ option (long listing format)/home→ argument (which directory to list)
If something is optional, it may be omitted. For instance, ls with no arguments lists the current directory.
Options: short vs long
Options come in two common styles:
- Short options: single
-and a single letter - Example:
-l,-a,-r - Long options: double
--and a word - Example:
--all,--recursive,--help
Many commands support both:
ls -a
ls --allShort options can often be grouped:
ls -la # same as ls -l -aLong options cannot be grouped; each must stand alone:
grep --ignore-case --recursive "pattern" .Some options take a value:
head -n 20 file.txt # show first 20 lines
head --lines=20 file.txt
Whether you use -n 20, -n20, --lines 20, or --lines=20 depends on the specific command’s rules (see --help or the man page).
Arguments and their order
Arguments are usually:
- filenames or directory names
- patterns or “what to search for”
- destinations (target directories, output files, etc.)
Very often, the order matters.
Example: cp (copy file):
cp source.txt destination.txt- First argument: what to copy (source)
- Second argument: where to copy to (destination)
Reversing them does something different or may fail:
cp destination.txt source.txt # overwrites source.txt with destination.txtMany commands expect:
- options first, then
- arguments afterwards
Example:
grep -i "hello" file.txt
Occasionally, you’ll see -- used to end options so later words are always treated as arguments:
rm -- -weirdfilename
Here -- tells rm that -weirdfilename is a file name, not an option.
Whitespace and quoting
The shell splits the command line on spaces and tabs:
cp my file.txt backup/This becomes:
cpmyfile.txtbackup/
To treat text containing spaces as one argument, you must quote it:
cp "my file.txt" "backup folder/"or
cp 'my file.txt' 'backup folder/'
Single quotes ('…') and double quotes ("…") both group words, but handle special characters differently (you’ll see the details later); for now:
- Use quotes anytime an argument contains spaces or special characters.
- Example:
mkdir "My Documents"Special characters in command structure
Certain characters have special meanings in the shell and affect command structure:
;— separate multiple commands on one line&&— run next command only if previous succeeded||— run next command only if previous failed|— pipe (connect output of one command to input of another)>/>>/<— redirect input or output&— run command in background()and{}— group commands (with syntax rules)
These are not ordinary arguments; the shell interprets them to build compound commands.
Examples:
ls; pwd
Two commands separated by ;:
- run
ls - then run
pwdregardless of whetherlsworked
mkdir newdir && cd newdir- run
mkdir newdir - only if that succeeded, run
cd newdir
rm file.txt || echo "Failed to remove file"- try to remove
file.txt - if that fails, print a message
ls | less- run
ls - send its output via
|intoless
Redirection and pipes are covered more deeply elsewhere; here, focus on recognizing them as part of command structure.
Simple vs compound commands
Simple commands
A simple command is just:
- a program name (or built‑in)
- with optional options and arguments
Examples:
ls -l /etc
cat file.txt
echo "Hello"Compound commands
Compound commands use control operators or special syntax to combine simple commands:
- with
;,&&,||,|,&, etc. - with grouping:
(cd /tmp && ls)The parentheses run the grouped commands in a subshell.
- with
{ ...; }:
{ echo "First"; echo "Second"; }The details of control flow and pipes are discussed in later chapters; for now, recognize that adding these operators changes how the shell runs your commands.
Command structure and the PATH
When you type a command name (e.g. ls), the shell:
- Checks if it’s a built‑in (like
cd,echoin many shells). - If not built‑in, looks for an executable file with that name in the directories listed in the
PATHenvironment variable. - If not found, you get an error like
command not found.
If you provide a path instead of just a name, that path is used directly:
./script.sh # run script in current directory
/bin/ls # run ls explicitly from /binThis is part of how a command is resolved before it actually runs; the structure you type influences what the shell searches for.
Reading and “parsing” commands
When you see a new command, mentally break it down:
- What is the command?
- Which words are options, and what do they modify?
- Which words are arguments, and what are they applied to?
- Are there any special characters (pipes, redirects,
&&, etc.) changing the flow?
Example:
grep -i --color=auto "error" /var/log/syslog | lessBreakdown:
grep→ command-iand--color=auto→ options (case‑insensitive, colorized output)"error"and/var/log/syslog→ arguments (pattern, file)| less→ pipe output of the wholegrep ...command intoless
Thinking in this way makes unfamiliar commands much easier to understand and adapt.