Kahibaro
Discord Login Register

2.1.3 Command structure

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}]$$

Example:

ls -l /home

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:

Many commands support both:

ls -a
ls --all

Short options can often be grouped:

ls -la    # same as ls -l -a

Long 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:

Very often, the order matters.

Example: cp (copy file):

cp source.txt destination.txt

Reversing them does something different or may fail:

cp destination.txt source.txt   # overwrites source.txt with destination.txt

Many commands expect:

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:

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:

  mkdir "My Documents"

Special characters in command structure

Certain characters have special meanings in the shell and affect command structure:

These are not ordinary arguments; the shell interprets them to build compound commands.

Examples:

ls; pwd

Two commands separated by ;:

bash
mkdir newdir && cd newdir
bash
rm file.txt || echo "Failed to remove file"
bash
ls | less

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:

Examples:

bash
ls -l /etc
cat file.txt
echo "Hello"

Compound commands

Compound commands use control operators or special syntax to combine simple commands:

bash
  (cd /tmp && ls)

The parentheses run the grouped commands in a subshell.

bash
  { 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:

  1. Checks if it’s a built‑in (like cd, echo in many shells).
  2. If not built‑in, looks for an executable file with that name in the directories listed in the PATH environment variable.
  3. 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:

bash
./script.sh      # run script in current directory
/bin/ls          # run ls explicitly from /bin

This 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:

Example:

bash
grep -i --color=auto "error" /var/log/syslog | less

Breakdown:

Thinking in this way makes unfamiliar commands much easier to understand and adapt.

Views: 81

Comments

Please login to add a comment.

Don't have an account? Register now!