Understanding Command Syntax and Arguments
Almost every Bash command follows the same pattern: a command name, optional settings called options, and the things the command should work on, called arguments. Once you understand this pattern, you can read the manual page of an unfamiliar command and know how to use it. This lesson explains each part, how Bash splits a line into words, and how to pass values that contain spaces or look like options.
Understanding the Concept
A typical command line has this shape:
command [options] [arguments]
- The command is the first word. It names the program or built-in to run.
- Options change how the command behaves. They usually start with a dash, as in
-l. - Arguments are the data the command acts on, such as a file name or a piece of text.
For example:
ls -l /etc
Here ls is the command, -l is an option that asks for a long listing, and /etc is the argument that says which directory to list.
The terms are sometimes used loosely. In the technical sense, everything after the command name is an argument, and options are a particular kind of argument that the command interprets as a setting. This lesson uses the common distinction, because it is more useful to learners.
How Bash Splits a Line into Words
Before it runs anything, Bash breaks the line into words. Spaces and tabs separate them. Bash then passes the first word to the system as the command name, and the other words as the list of arguments.
A small function makes this visible. The function below prints each argument it receives inside square brackets:
show() { printf '[%s]\n' "$@"; }
You will learn functions properly later. For now, accept that show prints one line per argument. Use it to see how Bash splits text:
show one two three
Output:
[one]
[two]
[three]
Three arguments arrived. The extra spaces between two and three were only separators, so they disappeared. Now put the same text in quotes:
show "one two three"
Output:
[one two three]
Quotes turn the whole text into one argument and keep the spaces. This is the most important idea in the lesson: the command never sees your spaces, only the words that Bash made from them.
Options
Options adjust what a command does. They come in two styles.
Short Options
A short option is a dash followed by one letter. The ls command has many of them:
ls -l
Output in a directory that contains two files:
total 0
-rw-r--r-- 1 student student 0 Nov 20 10:52 a.txt
-rw-r--r-- 1 student student 0 Nov 20 10:52 b.txt
-l selects the long listing format, which shows permissions, owner, size, and date.
You can write several short options separately or combined into one word. The two commands below mean the same thing:
ls -l -a
ls -la
In both cases ls lists in the long format (-l) and includes hidden files (-a). The order of combined letters rarely matters, so -al works too.
Long Options
A long option begins with two dashes and uses a whole word:
ls --all
Output in an empty directory:
. ..
--all is the long form of -a. Long options are easier to read in scripts, because they describe themselves. Many GNU commands accept both forms, but some commands have only short options, so check the command's help.
Options That Take a Value
Some options need an extra value. The head command prints the first lines of a file, and -n tells it how many lines to print. Create a sample file first:
printf 'l1\nl2\nl3\nl4\nl5\n' > nums.txt
head -n 2 nums.txt
Output:
l1
l2
The printf line writes five lines into nums.txt, and the redirection symbol > is explained in a later lesson. The head -n 2 command prints the first two lines. The value can be attached in several ways, and all of the following print the same result with GNU head:
head -n 2 nums.txt
head -2 nums.txt
head --lines=2 nums.txt
The long form joins the option and its value with an equals sign. The -2 shortcut is an older style that GNU head still accepts, but -n 2 is the more portable form.
Arguments
Arguments tell the command what to work on. Different commands expect different kinds and numbers of arguments.
pwd takes no arguments, ls accepts zero or more, and cp needs at least two, a source and a destination. A command that gets the wrong number of arguments usually prints a short usage message.
Some commands accept many arguments of the same kind. wc -l counts lines, and it can count several files at once:
wc -l nums.txt
Output:
5 nums.txt
When a command reads from a file name argument, it prints the name beside the count. If you send the file through standard input instead, the name is omitted:
wc -l < nums.txt
Output:
5
The < symbol feeds the file to the command as input, so wc never learns the file name. Redirection has its own lesson.
Arguments That Contain Spaces
A file name with a space in it must be a single argument. Two ways of writing it are quoting and escaping:
show "my file.txt"
show my\ file.txt
Output of each:
[my file.txt]
In the first form the quotes hold the text together. In the second form the backslash removes the special meaning of the space that follows it. Both produce one argument. Without either, the name would be split into my and file.txt, and the command would look for two different files.
Quotes can also protect other characters. Single quotes keep everything exactly as typed:
show 'a "quoted" word'
Output:
[a "quoted" word]
Arguments That Look Like Options
A file name can start with a dash, which causes confusion because the command may treat it as an option. Create such a file by telling touch that the options have ended:
touch -- -weird.txt
ls
Output:
-weird.txt
The double dash -- is a widely used convention. It means "no more options follow, so treat everything after this as ordinary arguments". The same trick lets you use the file safely:
ls -- -weird.txt
rm -- -weird.txt
Output of the ls command:
-weird.txt
Without the --, a command like rm -weird.txt would try to read -weird.txt as a group of options and fail. The convention works with most standard commands, but it is a habit of the commands rather than a Bash rule.
Case Sensitivity
Command names, options, and file names are all case-sensitive. The option -s and -S are different, and ls is a command while LS is not:
LS
echo "status=$?"
Output in an interactive shell:
bash: LS: command not found
status=127
Reading the Help for a Command
Most commands describe their own syntax. The common ways to ask are:
ls --help
Output (the first lines):
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
The line starting with Usage: is a compact summary of the syntax. Square brackets mean "optional", and three dots mean "can be repeated". So ls [OPTION]... [FILE]... reads as: the command ls, followed by any number of options, followed by any number of file names. You can also open the full manual page with man ls, which a later lesson explains.
Putting It Together
Combine an option, an option with a value, and an argument in one command:
mkdir -p demo
cd demo
touch a.txt b.txt
ls -1
Output:
a.txt
b.txt
How the lines work:
mkdir -p demohas the commandmkdir, the option-p(create parent directories if needed and do not complain if the directory exists), and the argumentdemo.cd demohas the commandcdand the argumentdemo, which moves the shell into the new directory.touch a.txt b.txthas two arguments, so it creates two empty files.ls -1uses the option-1(the digit one) to print one name per line.
If you ask for something that does not exist, the command reports an error and sets its exit status:
ls missing.txt
echo "status=$?"
Output in an interactive shell:
ls: cannot access 'missing.txt': No such file or directory
status=2
The command name, option, and argument all had correct syntax, so the failure comes from the data, not the grammar. Telling those two causes apart, a syntax problem versus a missing file, is the first step of troubleshooting any command.