Anatomy of a command
A command line is a list of words. See how the shell splits them, who reads options, why -- ends them, and where macOS BSD tools differ from GNU.
Anatomy of a command
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The shell splits a command line into words on whitespace. The first word names the command and the rest are handed to it as arguments.
- Options such as -l or --long are not special to the shell. They are ordinary arguments that the command chooses to interpret.
- Short options can usually be combined (-la) and may take a value (-n 1 or -n1). Long options with two dashes are not guaranteed to exist on every system.
- A bare -- means the options are over, which lets you name a file that starts with a dash.
- A command the shell can't find exits with status 127. Quote an argument that contains spaces so it stays one word.
A command line is a list of words
You type ls -l notes.txt and something happens. The first thing the shell does is boring and important: it cuts the line into words, splitting wherever there is whitespace. The first word is the command. Every later word is an argument that gets passed to that command. Nothing more is going on.
You can watch the splitting happen. printf '[%s]\n' prints each of its arguments inside brackets, one per line, so it shows you exactly how many words arrived:
$ printf '[%s]\n' hello world "hello world"
[hello]
[world]
[hello world]
Three spaces between hello and world collapsed to nothing, so two words arrived. The quoted version stayed one word with its spaces intact. That is the reason a file called my notes needs quotes: without them rm my notes asks for two files. A later lesson, quoting, covers the rules properly. For now, remember that a space separates words unless you quote it.
Options are just arguments
Words that start with a dash are called options or flags. They adjust what the command does: ls -l asks for a long listing, head -n 1 asks for one line.
The surprise is that the shell doesn’t know any of this. It passes -l to ls like any other string, and ls decides what it means. Which is why -n means “number of lines” to head and something else to another tool.
Conventions hold across most tools:
- A short option is one dash and one letter. They combine, so
ls -l -aandls -laare the same thing. I ran both and got identical listings. - A short option can take a value. Both
head -n 1 a.txtandhead -n1 a.txtprinted the first line. - A long option uses two dashes and a word, like
--color.
Here’s a thing to know on a Mac. ls -l shows a trailing @ after the permissions when a file has extended attributes:
$ ls -la | head -3
total 16
-rw-r--r--@ 1 me wheel 4 Nov 23 02:37 -v
drwxr-xr-x@ 4 me wheel 128 Nov 23 02:37 .
That @ is a macOS detail and won’t appear on Linux.
BSD tools and GNU tools differ
Linux distributions usually ship the GNU versions of ls, sed, grep and friends. macOS ships BSD versions, descended from a different codebase. They overlap on the basics and drift apart on details. The long-option form is the most common trip:
$ ls --all
ls: unrecognized option `--all'
usage: ls [-@ABCFGHILOPRSTUWXabcdefghiklmnopqrstuvwxy1%,] [--color=when] [-D format] [file ...]
$ date --iso-8601
date: illegal option -- -
usage: date [-jnRu] [-I[date|hours|minutes|seconds|ns]] [-f input_fmt] ...
Both are fine on GNU/Linux. The portable choice is the short form (ls -a), or on date the format string, date -u +%F. Throughout this roadmap I ran everything with the BSD tools that come with macOS, and I say so wherever a flag differs. When a lesson shows --something, check it exists on your system before copying it.
Ending the options with --
What if a file’s name starts with a dash? Make one:
$ printf 'two\n' > -v
$ cat -- -v
two
$ cat ./-v
two
$ rm -- -v
cat -v would read -v as an option. A bare -- says “no more options, everything after this is a name”, and ./-v works because the name no longer starts with a dash. Without either, you get confusing behaviour or, with rm, a command that deletes the wrong thing. Scripts that handle filenames from outside should always use --.
Variables before a command, and the failing case
A command can have variable assignments in front of it that apply to that command alone:
$ FOO=bar env | grep ^FOO
FOO=bar
And when the first word isn’t a command at all, the shell reports it and exits with a status that scripts can test:
$ bash -c 'nosuchthing'
bash: line 1: nosuchthing: command not found
$ echo $?
127
Status 127 means “command not found”, which a later lesson on exit status builds on. Interactively the message begins bash: without the line number.
About the video
This is CodeLucky’s “Linux Command Structure for Beginners: Syntax, Options & Arguments Explained!”, published 6 April 2025, 5 minutes 45 seconds. The description lists command syntax, short and long options, arguments, and getting help, with chapters at 0:15 basics, 1:05 command name, 1:53 options, 2:55 arguments and 3:49 getting help. I couldn’t read a transcript, so I haven’t checked what it says about -- or about BSD against GNU, and I wouldn’t expect it to cover them. The channel offers dubbed audio tracks in many languages, so the English judgement rests on the English title and description, not on hearing it. The video targets Linux, so some option spellings in it may not work on a Mac.
What I did not run
Everything above was run on macOS with Bash 5.3 and BSD userland. I did not run the examples on GNU/Linux, so what I say about GNU ls accepting --all is the standard behaviour, not something I saw.
Next
You know what a command is made of. The next question is what to do when you don’t know a command’s options: getting help with man, help and type.
Check yourself
What does the shell do with the line `ls -l my file`?
It splits on runs of whitespace into four words: ls, -l, my, file. The command ls receives three arguments, and my file is looked up as two separate names.
You have a file named -v and `cat -v` doesn't print it. How do you read it?
Use cat -- -v or cat ./-v. The -- ends option parsing and ./ makes the name no longer start with a dash.
Who decides that -l means long format: the shell or ls?
ls. The shell passes the string -l through untouched, and each program parses its own options, which is why meanings differ between programs.
`ls --all` works on your Linux server but fails on your Mac. Why?
GNU ls on Linux accepts long options like --all. macOS ships BSD ls, which only knows single-letter options, so the equivalent there is ls -a.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.