Course outline · 0% complete

0/29 lessons0%

Course overview →

Anatomy of a command

lesson 1-3 · ~8 min · 3/29

Command, options, arguments

Learning this one pattern pays for itself forever: every manual page, tutorial, and Stack Overflow answer assumes you can read it. Once you see the parts, an unfamiliar command stops being a magic string and becomes name + settings + target.

Almost every command you'll ever type has the same three-part shape:

ls -l /tmp
  • Command name (ls): which program to run.
  • Options (also called flags), like -l: switches that change how the program behaves. They start with - (short form) or -- (long form, like --help).
  • Arguments (/tmp): what the program should act on. A file, a directory, some text.

You met this already: in echo "Hello, world" (lesson 1-1), echo is the command and the quoted text is its argument. Options are optional, and many commands also work with no arguments at all (pwd, ls).

grep -i error log.txt command name which program runs option / flag changes HOW it behaves arguments WHAT it acts on
The command grep -i error log.txt broken into its parts. grep is labelled the command name that decides which program runs, -i is highlighted as the option or flag that changes how it behaves, and error and log.txt are labelled as the arguments naming what the command acts on.

A flag that changes echo's behavior

echo normally ends its output with a newline. The -n flag suppresses that newline, so whatever prints next continues on the same line.

echo "one"
echo -n "two"
echo "!"

Output

one
two!

The first echo behaves normally, so one gets its own line. The second carries -n, so after printing two the cursor stays put, and the third echo writes ! right where the cursor sits. That is why two and ! end up glued together as two!.

When you don't know a command

Two built-in ways to learn, no Google needed:

  • commandname --help: most commands print a short usage summary.
  • man commandname: opens the full manual page in a scrollable reader. Move with the arrow keys or space, and press q to quit (everyone gets stuck in man exactly once).

You don't memorize commands. You remember they exist and look up the details. man ls will show you a dozen flags, and you'll use about four of them, ever.

In the command grep -i error log.txt, the option is -i. Options are recognizable by their leading dash, and this one tells grep to ignore case when matching, a flag covered properly in unit 4.

The other three pieces play different roles. grep is the command name, the program being run. error and log.txt are arguments: the pattern to search for and the file to search in, the things grep acts on.

To leave a manual page and return to the prompt, press q.

Man pages open inside a pager program called less, which takes over the whole screen to let you scroll. While you are in there, the space bar and arrow keys move through the text, and q quits the pager and hands the screen back to the shell.

Because -n suppresses the trailing newline, a pair of echo commands can build a single line of output between them. The first prints the prefix without ending the line, and the second finishes it.

echo -n "loading..."
echo "done"

Output

loading...done

Note where the flag sits: between the command name and its argument, as echo -n "text". Since the first echo never emitted a newline, the second one continues on the same row, and its own trailing newline closes the line at the end.