Where to start First steps track · 15 stages
Linux Labs

How a command is built — options and arguments

5 min Ubuntu 24.04 · Bash 5.2

A terminal line is a command name, options (-l, --all) and arguments; the shell splits it into words at spaces.

Cheat sheet

10 commands Printable version
  • ls -l -a /etc a command, two options, an argument
  • ls -la /etc short options can be combined
  • ls --all --human-readable long options
  • head -n 5 app.log an option with a value
  • head --lines=5 app.log a long option with a value
  • cp "my file.txt" docs/ a space in the name — quote it
  • rm -- -file.txt after -- only arguments
  • ls --help | less short help on options
  • man ls the full manual
  • mkdir logs && cd logs the second runs if the first succeeded

Overview

The shell (bash) splits a line into words at spaces. The first word is the command name; the rest are arguments that the program receives as they are:

text
student@ubuntu:~$ printf '[%s]\n' one "two three" four
[one]
[two three]
[four]

The quotes glued two three into a single argument. Double quotes still expand variables ("$HOME"); single quotes keep the text as is.

By convention, arguments starting with a dash are options: they change how the command behaves. The rest are what the command works on: files, directories, names.

Form What it is
-l a short option, one letter
-la several short options together: -l -a
--all a long option, a word
-n 5, --lines=5 an option with a value
-- end of options: only arguments follow

Only the command itself knows which options it has: see command --help and man command (see man-help). The shell expands patterns like *.log and ~ before the program starts, so the program gets ready names — see globbing.

Recipes

4

A file name with a space or special character

bash
cat "May report.txt"         # quotes
cat May\ report.txt          # or \ before each space

The easiest way is to type the start of the name and press Tab: the shell completes and escapes it.

An argument that starts with a dash

bash
rm -- -rf.txt                # -- separates options from arguments
grep -e '-v' notes.txt       # search for the string "-v"

Several commands on one line

bash
date; uptime                         # one after another, always both
mkdir -p build && cd build           # the second only on success
ping -c1 10.0.0.1 || echo 'no link'  # the second only on failure

A long command over several lines

bash
sudo useradd -m -s /bin/bash \
  -c 'Alice Smith' -G sudo alice

A \ at the end of a line continues the command on the next one. There must be no space after the \.

Troubleshooting

5

invalid option -- 'z' / unrecognized option '--foo'

The command has no such option. Check the spelling in command --help. Keep in mind that the same command's options differ between Linux, macOS and BusyBox.

missing operand

The command lacks an argument: mkdir with no name, cp with a single file. Add what to create or where to copy.

No such file or directory for half a name

The shell split a name with a space into two arguments. Quote the name or complete it with Tab.

command not found

The first word isn't a command the shell knows: a typo, the program isn't installed, or it isn't in PATH. Check type name — see env-vars and bash.

The quotes aren't closed — the prompt changed to >

The shell waits for the closing quote. Type it and press Enter, or cancel the input with Ctrl+C.

Terms

2
command argument
A word on the command line after the command name that the shell passes to the program. The shell splits the line into arguments on whitespace; quotes keep words with spaces in one argument. Also appears in: test, [ ] and [[ ]]
command option
An argument that changes a program's behavior; it usually starts with - (short form, -l) or -- (long form, --all). The argument -- usually marks the end of options. Options are listed in man and --help. Also appears in: bash, top

See also

  • bash — the shell that parses the command line.
  • man-help — finding a command's options: --help, man, apropos.
  • globbing — *, ?, {}: what the shell substitutes before running.
  • env-vars — variables and PATH, where the shell looks for commands.
  • alias — short names for long commands.
  • redirection — >, | and 2>&1 on the same line.
Loading…