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:
bash -c 'echo "argv[0] is $0"' my-nameThe 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.
-lameans 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 2and-n2are valid forhead. - 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:
seq 5 | head -n 2
seq 5 | head -n2Both 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_longalso accepts any unambiguous prefix of a long option name.ls --almostmeans--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:
touch -- -weird
ls -l -- -weird
rm -- -weirdWithout 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:
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 xThe 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:
show() { printf '[%s]\n' "$@"; }
show -la "two words" -- -weirdThe output is:
[-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:
ls -la /workspace
ls -l -a /workspaceBoth 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.