$_ CLI In Depth
Learning progress 0%
lesson terminal shell kernel

Arguments, Options & Utility Conventions

6 min read Module 1: Terminal, Shell & Kernel Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:16

What a program actually receives

When you type ls -la /workspace and press Enter, the shell does not hand that line to ls as one string. It splits the line into words using whitespace and quoting rules, performs its expansions, and then asks the kernel to start the program with the execve(2) system call. That call takes the path of the program, an array of strings called the argument vector (argv), and an array of environment strings. The kernel copies these arrays into the new process unchanged. It never looks inside them.

For the command above, argv holds three entries: ls, -la, and /workspace. The option -la is one entry, not two. To the kernel it is just three characters, and only ls decides that they mean "long listing" and "show hidden files".

By convention, argv[0] is the name the program was started under, and the program may read it. The caller chooses that value, not the program. You can see this directly:

BASH1 lines
bash -c 'echo "argv[0] is $0"' my-name

The output is argv[0] is my-name. Nothing about my-name is a file or a command. It is simply the first element that bash placed in the argument vector for the script.

Who decides what an option means

Options are not a shell feature. Neither the shell nor the kernel knows that -r is special. The program parses argv, decides which entries are options, and reacts to them. This is why the same letter can mean different things in different tools: -r means recursive in cp and grep, but reverse order in sort. If you remember only one thing from this lesson, remember this. A flag is a request that a specific program understands, and you must read that program's documentation to know what it accepts.

The shell does affect what the program receives in one important way. Pathname expansion (globbing) happens before the program starts, so a pattern such as * is replaced by the matching file names. If a file in the directory happens to be named -n, the program gets -n as an ordinary argument and may treat it as an option. The section on ending option parsing with -- shows how to handle that.

The POSIX conventions for short options

The POSIX standard describes how most command-line utilities should parse their arguments. The guidelines are in the Utility Conventions chapter, and they define the vocabulary you will see in every man page. The important rules are these:

  • An option is a hyphen followed by a single letter or digit, such as -a.
  • Options that take no argument can be grouped after one hyphen. -la means the same as -l -a.
  • An option that takes an option-argument reads its value from the next word, or from the rest of the same word. Both -n 2 and -n2 are valid for head.
  • The operands are the remaining non-option arguments, usually file names or values.
  • The word -- ends option processing. Everything after it is an operand, even if it starts with a hyphen.

Try the option-argument forms:

BASH2 lines
seq 5 | head -n 2
seq 5 | head -n2

Both commands print 1 and 2. The POSIX guidelines also say options should come before operands. GNU tools, which Linux systems use, relax that rule by default. They scan the whole argument list, so ls /workspace -l works. Scripts that must be portable should still put options first.

Long options and the GNU style

POSIX has only short options. GNU programs also accept long options with two hyphens and a word, such as --all or --color=never. Long options are much easier to read in scripts. The rules for arguments are:

  • For a long option with a required argument, you can write the value after an equals sign (--color=never) or as the next word (--color never). The equals form is the one to use in scripts.
  • Some long options take an optional argument. For those, only the equals form works, because a following word could be a file name.
  • GNU getopt_long also accepts any unambiguous prefix of a long option name. ls --almost means --almost-all. Abbreviations are convenient at a prompt, but write the full name in scripts because a later release may add a longer option with the same prefix.

Ending option parsing with --

Suppose a directory contains a file whose name starts with a hyphen. A name like -weird looks like a bundle of short options to any tool that scans argv. The -- marker tells the program that no more options follow. Two common forms work the same way:

BASH3 lines
touch -- -weird
ls -l -- -weird
rm -- -weird

Without the marker, touch -weird is parsed as the options -w, -e, and so on. GNU touch rejects the first letter it does not know, and you see an error similar to touch: invalid option -- 'w' followed by a hint to run touch --help. An alternative to -- is to name the file with a path prefix, such as ./-weird. The name no longer starts with a hyphen, so no tool can read it as an option.

Parsing options yourself: getopt and getopts

If you write a tool, you do not have to implement this from scratch. The C library provides getopt(3), which scans argv for short options and returns one option at a time. GNU systems also provide getopt_long for long options. Bash has a builtin called getopts that does the same job for shell functions and scripts. Run help getopts in the lab to read its short description. A small example:

BASH2 lines
f() { local OPTIND=1 o; while getopts "ab:" o; do echo "opt=$o arg=${OPTARG-}"; done; shift $((OPTIND-1)); echo "operands: $*"; }
f -a -b file1 x

The output is three lines. -a takes no argument. -b takes one, and bash stores it in OPTARG. After the loop, shift removes the parsed options so that only operands remain. The local OPTIND=1 line gives each call a fresh starting point, which matters when the function is called more than once.

Hands on: see what a program receives

A small function that prints each argument in brackets is the clearest way to see word boundaries. The "$@" form keeps every argument as one word, including arguments that contain spaces:

BASH2 lines
show() { printf '[%s]\n' "$@"; }
show -la "two words" -- -weird

The output is:

TEXT4 lines
[-la]
[two words]
[--]
[-weird]

The function itself does not know about --. It receives the marker as an ordinary argument and passes it along. Only a program that parses options would treat it as a terminator. Now compare the two ways of writing a long listing:

BASH2 lines
ls -la /workspace
ls -l -a /workspace

Both commands print the same output, because the grouped form and the separated form produce the same options for ls. The shell does not care which form you use. Only ls decides what it means.

Exit status, && and ||

Visualizer

Every command ends with a number from 0 to 255. Zero means success; everything else is a kind of failure, and &&, || and if branch on it.

1 / 6
$ true; echo $?
0
status
0
meaning
success

true does nothing and exits 0. Zero is the only success value.

true; echo $?; false; echo $?; ls /nope; echo $?; nosuchcmd; echo $?
Terminal lab · 3 steps · real Linux in your browser

Control exactly what reaches argv

Remove a file that looks like an option, pass precise arguments, and use a GNU long option.

01
Remove the file named -n
There is a file named -n in /workspace. Delete it, and only it. rm -n fails because rm reads -n as an option.
Show hint for step 1
`--` ends the options, so `rm -- -n` works. So does `rm ./-n`.
02
Exactly three arguments
./showargs prints each argument it receives in brackets. Make it receive exactly these three arguments: a b (with the space), -c, and d. Save the output: ``bash ./showargs ... > args.txt ``
Show hint for step 2
Quote the first argument: `./showargs 'a b' -c d > args.txt`.
03
A GNU long option
List the files in sizes/, one name per line, largest first, using the long option --sort. Save it as sorted.txt. Check ls --help for the values --sort accepts.
Show hint for step 3
`ls -1 --sort=size sizes > sorted.txt`
Check yourself Question 1 of 4
Concept Check What argv holds recall

When you run ls -la /workspace at the prompt, what does the kernel receive as the program's argument vector?

Recap & Key Takeaways

The kernel passes a program its argument vector (argv) as an array of strings and does not interpret them; the program's own code decides what each option means.
POSIX short options are a hyphen and one letter, can be grouped (-la), take option-arguments as the next word or the rest of the same word, and end at --.
GNU long options use --name or --name=value; use the equals form in scripts and write full option names rather than abbreviations.
Use -- or a ./ prefix to pass a file whose name starts with a hyphen, such as touch -- -weird and rm -- -weird.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue