Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 95 ยท Getting Started

The Shell: Commands, Arguments and Options

Learn how a shell command is built - command, options and arguments - plus command types, exit status and chaining with semicolons, && and ||, with live Bash examples.

Every task you do on the command line follows the same pattern: you type a line, the shell splits it into words, treats the first word as the program to run and hands the rest to it. Once you understand that single rule, the hundreds of commands you will learn stop looking like random incantations and start looking like variations on one theme.

This lesson covers the anatomy of a command, how options differ from arguments, why spaces and capital letters matter, what kinds of commands exist, how a command reports success or failure with its exit status, and how to run several commands in a row. It builds on Getting Started with Linux. Afterwards you will learn where to find help in Getting Help.

The anatomy of a command

A command line is a list of words separated by spaces. The first word is the command (what to run). The remaining words are passed to it, and they come in two flavors:

  • Options (also called flags or switches) change how the command behaves. They start with a dash.
  • Arguments say what the command should work on: a file name, some text, a number.
TEXT
   ls  -l  -h   /etc
   |   |   |    |
   |   |   |    +-- argument: what to list
   |   +---+------- options:  how to list it
   +--------------- command:  the program to run

The shell does not understand ls, -l or /etc. It only splits the line into the words ls, -l, -h and /etc, finds a program called ls and starts it with the other three words as its argument list. The program itself decides what each word means. That is why option conventions differ a little between tools, even though most follow the same habits.

Let's use echo, the simplest command, to see the splitting. echo prints its arguments separated by single spaces:

Bash The shell splits a line into words

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first two lines print exactly the same thing: one two three. The shell throws away the extra spaces, because spaces only separate words, and echo receives three arguments in both cases. The third line puts the text in double quotes, so the shell passes one argument that contains the spaces, and they survive. Quoting is how you override the splitting, and we study it in depth in Quoting and Expansion.

Options: short and long

Most commands accept options. There are two styles:

  • Short options are a single dash and a single letter: -l, -a, -h. You can usually combine them: -lah means -l -a -h.
  • Long options are two dashes and a word: --all, --human-readable. They are longer to type but self-explanatory, so they are the better choice in scripts you will read again in six months.

Some options take a value of their own, either after a space or after an equals sign: head -n 2 or head --lines=2. A lone -- means "no more options after this point", which matters when a file name might start with a dash.

Bash Short, combined and long options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Plain ls hides names that start with a dot. -a shows all of them, including the special entries . and .., while -A shows hidden files but not those two. Notice that ls -r -1 and ls -r1 do exactly the same, and the long spelling is the most readable. You will take a longer tour of ls in Navigating: pwd, cd and ls.

Option letters are defined by each program. -r means "reverse" for ls, "recursive" for cp and grep, and something else again for other tools. Do not guess across commands; look it up (see Getting Help).

Case and spaces matter

Linux is case sensitive. ls, LS and Ls are three different names, and so are the files Notes.txt and notes.txt. The shell also treats a space as a separator, so a file called my notes.txt needs quotes or an escaping backslash to be a single argument. Both mistakes produce the same kind of error message, and the message is worth learning to read:

Bash Errors you will see a lot (fails on purpose)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

ECHO hello fails with main.sh: line 1: ECHO: command not found (at a prompt the prefix is just bash:), because no program has that exact name. Listing a path that does not exist prints ls: cannot access '/does/not/exist': No such file or directory. The cat my notes.txt line is the classic: the shell sees two arguments, my and notes.txt, and neither is a file. Quote it as cat "my notes.txt" and it works. (We append 2>&1 so that the error messages appear in the output in order; Standard Streams and Redirection explains why.) The last line prints the exit status, which is the next topic.

What counts as a command

The first word can refer to four different kinds of things. The type command tells you which one you are dealing with:

  • A builtin is part of Bash itself, such as cd, echo and read. Builtins are fast and can change the shell's own state (cd must be a builtin, because a separate program could not change the shell's directory).
  • A file is an external program found in one of the directories listed in the PATH variable, such as /bin/ls.
  • An alias is a short nickname you define for a longer command line, mainly for interactive use.
  • A function is a named block of shell code you define yourself (see Functions).
Bash Asking the shell what a name means

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

type cd says cd is a shell builtin, while ls is an external file at /bin/ls. type -t prints just one word describing the kind: builtin, file, function, alias or keyword. Interestingly, echo exists both as a builtin and as /bin/echo; type -a lists every match and the shell uses the first one. When the same name exists in several places, this order of lookup is what decides which version runs.

Exit status: success or failure

Every command ends with a number from 0 to 255 called its exit status. By convention 0 means success and anything else means some kind of failure. The shell stores the status of the most recent command in the special variable $?. This is the opposite of most programming languages, where 0 is false; in the shell, "zero errors" is true.

Bash Reading the exit status

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

true and false are tiny programs that do nothing but succeed or fail. ls on a missing path returns 2, and grep returns 0 when it finds a match and 1 when it does not; the -q option makes it print nothing, so only the status matters. The 2>/dev/null discards the error message. Exit statuses are how scripts make decisions, which is the subject of Conditionals and Exit Codes and Error Handling.

Running several commands

You can put more than one command on a line, or on consecutive lines, and control how they relate:

Operator Meaning
newline or ; Run the next command regardless of the previous one
&& Run the next command only if the previous one succeeded (status 0)
|| Run the next command only if the previous one failed
& Start the command in the background (see Jobs)
| Send the output of one command into the next (see Pipes)
Bash Chaining with semicolon, AND and OR

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The first mkdir succeeds, so the && branch runs. The second mkdir fails (the directory exists), so the text after && is skipped, and mkdir prints an error to the error stream. The third uses || to react to that failure. Be careful with A && B || C: if B itself fails, C runs too, so it is not a true if-else. Use if for that (Conditionals).

Everything from a # to the end of the line is a comment that the shell ignores. Use comments to explain why, not to repeat what the command already says.

Typing a long command wrong is normal. In an interactive shell, the Up arrow recalls earlier lines, Ctrl+C cancels the running command, Ctrl+L clears the screen and Ctrl+A/Ctrl+E jump to the start and end of the line. These are shown read-only here because our editor does not have an interactive prompt.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In ls -l /etc, which word is the argument?

    ls is the command, -l is an option that changes how it lists, and /etc is the argument saying what to list.
  2. 2. What does echo a b print?

    The shell splits on whitespace and discards the extra spaces. echo receives two arguments and joins them with one space.
  3. 3. What does an exit status of 0 mean?

    By convention 0 means success; any other number signals a kind of failure. A missing command gives status 127.
  4. 4. Which command prints what kind of thing a name is (builtin, file, alias, function)?

    type name describes how the shell would interpret the name, and type -t prints only the kind.
  5. 5. What happens with mkdir work && echo done if work already exists?

    mkdir fails with a non-zero status, so && skips the echo.

Summary

  • The shell splits a line into words; the first word is the command, options change its behavior, arguments say what to work on.
  • Short options (-l) can be combined (-lah); long options (--all) are clearer in scripts. -- ends option parsing.
  • Everything is case sensitive, and a space separates words; quote text that contains spaces.
  • type tells you whether a name is a builtin, an alias, a function or a file.
  • Exit status 0 means success; $? holds the last status.
  • ; runs commands in sequence, && runs the next only after success, || only after failure, and # starts a comment.

Next: Getting Help: man, --help and Learning Commands