By now you should be familiar with running shell commands.
If you are fortunate, you should have noticed a pattern in how you provide them.
The general themes are:
Short options of a single character after a hyphen,
Possibly followed by a second argument as a value to the argument, or the value to the option immediately following.
So
-ofoois equivalent to-o foo.Multiple short options that don't take a value argument grouped into the same command.
So
-abcand-a -b -care equivalent.
When combining short option groups and short options with value arguments, you can provide as many no-value options in the same command as you like but the string after the first option with a value becomes the value for that option.
So
-vvofoois equivalent to-v -v -o foo, and if you want to provide multiple-ooptions, the shortest way to express that is-vvofoo -obar.Long options starting with two hyphens.
- Value-less options such as
--foo, often paired with a--no-foo. - Options with values,
either with the value as a separate command-line argument,
or separated by an equals,
so
--foo=baris equivalent to--foo bar.
- Value-less options such as
Any number of positional arguments after any number of option arguments.
Traditional posix behaviour has the first non-option argument being the last option with the rest being interpreted as positional arguments.
So with
--foo bar baz --quxis not equivalent to--foo bar --qux baz.The usual GNU behaviour is to scan all the options to look for options and letting you intersperse positional arguments with optional.
So
baz --foo baris equivalent to--foo bar baz.
You can force posix behaviour by setting the
POSIXLY_CORRECTenvironment variable.You can force arguments to be positional with the magic
--argument, so--foo bar -- baz --quxis not equivalent to--foo bar baz --qux.If you are writing shell scripts
--is very useful, since if you are providing values from external sources you should use--to prevent it interpreting your positional arguments as extra options which may change the behaviour.Try to remember this, otherwise if you have a file called
-fin a directory,alias rm='rm -i'won't prevent you from accidentally removing things if you runrm *instead ofrm -- *.Arguments split into comma-separated sub-options.
You may have noticed that few tools accept multiple values per option, the traditional work-around for this is to provide only one value argument, but split that up with comma (
,) or colon (:) characters.If it is comma separated, it may also be sub-option
KEY[=VALUE]pairs.If you are unfortunate, the values may be file paths, which is a problem, because file paths may contain colons.