The Shape of a Command

Almost every command has the same shape, its name, then options that change how it works, then arguments that say what to work on, with spaces between them, quotes around a name that has spaces, and Ctrl+C to stop it.

Novice · Unix · Linux · Updated

After this lesson you can

  • Name the parts of a command, which are the command, its options and its arguments
  • Put quotes around an argument that has spaces, so it stays one argument
  • Stop a command that is still running with Ctrl+C

Key takeaways

  • A command is its name, then its options, then its arguments, with a space between each, as in ls -l ~/terminal-practice.
  • Options change how a command works. A short one is a dash and a letter (-l); a long one is two dashes and a word (--all).
  • Arguments say what to work on. The shell splits them at spaces, so a name with spaces goes in quotes.
  • Ctrl+C stops a command that is still running and gives you back the prompt.

A tutorial for a tool you need says "run ls -l ~/terminal-practice". Before you run a command someone else wrote, it pays to read it, the way you read a recipe before you start cooking. The good news: almost every command has the same three-part shape, so once you can read one, you can read most of them.

Three parts: name, options, arguments

A command reads like a short instruction to a helper: what to do, how to do it, and what to do it to.

ls     -l     ~/terminal-practice
│      │      └── an argument: what to work on (this folder)
│      └── an option: how to do it (the long, detailed list)
└── the command: what to do (list)
  • The command comes first: the name of the program to run. ls lists what's in a folder.
  • Options change how it works. -l (read "dash L") asks for the long, detailed list. Options are also called flags.
  • Arguments say what to work on: here, which folder to list. Leave the argument out, and ls lists the folder you're in.

A space separates each part from the next. Capital letters matter too: LS isn't ls (bash answers command not found), and -a and -A are two different options.

Arguments, and the spaces between them

The shell splits every line at its spaces. echo prints its arguments back with one space between them, so you can watch the split happen:

echo Packing list:   sunscreen,   hat
Packing list: sunscreen, hat

The shell saw four arguments, Packing, list:, sunscreen, and hat, and the extra spaces between them were lost. Quotes say "everything in here is one argument", spaces and all:

echo "Packing list:   sunscreen,   hat"
Packing list:   sunscreen,   hat

That matters most with names. Make a folder called summer plans in your practice folder, then list it:

cd ~/terminal-practice
mkdir -p "summer plans"
ls
'summer plans'

ls shows the name in quotes so you can see where it starts and ends. Now leave the quotes off:

ls summer plans
ls: cannot access 'summer': No such file or directory
ls: cannot access 'plans': No such file or directory

ls was handed two arguments and looked for two folders. (On a Mac, and on some Linux systems, the messages are worded a little differently, but they say the same thing.) Without quotes, mkdir summer plans would have made two folders, summer and plans. That's why so many people name folders with hyphens, like summer-plans: there's nothing to quote.

Options: short, long and combined

Options start with a dash. Here's ls with -l, in the practice folder:

ls -l
total 4
drwxr-xr-x 2 sam sam 4096 Nov  9 03:31 'summer plans'

Same folder, more detail: who owns it, its size, when it changed (Looking Around: ls reads this line). Your date will be today's.

  • A short option is a dash and one letter: -l, -a. Several can share one dash: ls -la is the same as ls -l -a.
  • A long option is two dashes (read "dash dash") and a word: ls --all does the same as ls -a. Long ones are easier to read later; short ones are quicker to type.
  • -p in mkdir -p (short for "parents") means "make the folder, and any missing folders on the way to it, and don't complain if it's already there". Without it, a second mkdir "summer plans" stops with an error that ends in File exists. That's why the lesson's lines are safe to run twice.

Ctrl+C: stop what's running

Most commands finish and give the prompt back at once. Some keep going, and some wait for you. Type cat with nothing after it and press Enter: the prompt doesn't come back, because cat is waiting for you to type text. Hold Ctrl and press C:

$ cat
^C
$

^C (read "control C") is how the terminal shows the keypress, and the prompt is back. Ctrl+C asks the running command to stop now. It works on any command that's taking too long, such as sleep 100, which waits a hundred seconds. It won't undo what a command already did; it only stops it from doing more.

On a Mac, it's Ctrl+C too, not Cmd+C (Cmd+C copies). In Windows Terminal, Ctrl+C copies when text is selected, and stops the command when nothing is.

Check yourself

2 questions to try on the lesson page.

So what

Name, options, arguments: read any command in those three parts before you run it, quote names with spaces, and remember that Ctrl+C gets you out when something keeps running.

Key terms

Cheat sheets

Related