There are some utilities that accept a --
(double dash) as the signal for "end of options", required when a file name starts with a dash:
$ echo "Hello World!" >-file
$ cat -- -file
Hello World!
$ cat -file # cat - -file fails in the same way.
cat: invalid option -- 'f'
Try 'cat --help' for more information.
But some of those utilities don't show such an option in the manual page.
The man
page for cat
doesn't document the use (or validity) of a --
argument in any of the OS'es. This is not meant to be a Unix – Linux flame war, it is a valid, and, I believe, useful concern.
Neither cat
, mv
, ed
(and I am sure many others) document such an option in their manual page that I can find.
Note that ./-file
is a more portable workaround to the use of --
.
For example, the source
(dot) command (and written as .
) doesn't (generally) work well with an --
argument:
$ echo 'echo "Hello World!"' >-file
$ . ./-file
Hello World!
$ . -file
ksh: .: -f: unknown option
ksh: .: -i: unknown option
ksh: .: -l: unknown option
ksh: .: -e: unknown option
Usage: . [ options ] name [arg ...]
$ . -- -file # works in bash. Not in dash, ksh, zsh.
ksh: .: -file: cannot open [No such file or directory]
Best Answer
This is a POSIX requirement for all utilities, see POSIX chapter 12.02, Guideline 10 for more information:
POSIX recommends all utilities to follow these guidelines.
There are a few exceptions like
echo
(read at OPTIONS). And special builtins that do not follow the guidelines (likebreak
,dot
,exec
, etc.) :The intent is to document all commands that do not follow the guidelines in their POSIX man page, from POSIX chapter 12.02 third paragraph:
As the
cat
POSIX man page documents no deviations in the OPTIONS section, it is expected that it accept--
as a valid argument.There still may be (faulty) implementations that fail to follow the guideline.
In particular, most GNU core utilities follow this guideline