Why does rm manual say that we can run it without any argument, when this is not true?
The standard synopsis for the rm
utility is specified in the POSIX standard1&2 as
rm [-iRr] file...
rm -f [-iRr] [file...]
In its first form, it does require at least one file operand, but in its second form it does not.
Doing rm -f
with no file operands is not an error:
$ rm -f
$ echo "$?"
0
... but it just doesn't do very much.
The standard says that for the -f
option, the rm
utility should...
Do not prompt for confirmation. Do not write diagnostic messages or modify the exit status in the case of no file operands, or in the case of operands that do not exist. Any previous occurrences of the
-i
option shall be ignored.
This confirms that it must be possible to run rm -f
without any pathname operands and that this is not something that makes rm
exit with a diagnostic message nor a non-zero exit status.
This fact is very useful in a script that tries to delete a number of files as
rm -f -- "$@"
where "$@"
is a list of pathnames that may or may not be empty, or that may contain pathnames that do not exist.
(rm -f
will still generate a diagnostic message and exit with a non-zero exit status if there are permission issues preventing a named file from being removed.)
Running the utility with neither option nor pathname operands is an error though:
$ rm
usage: rm [-dfiPRrv] file ...
$ echo "$?"
1
The same holds true for GNU rm
(the above shows OpenBSD rm
) and other implementations of the same utility, but the exact diagnostic message and the non-zero exit-status may be different (on Solaris the value is 2, and on macOS it's 64, for example).
In conclusion, the GNU rm
manual may just be a bit imprecise as it's true that with some option (-f
, which is an optional option), the pathname operand is optional.
1 since the 2016 edition, after resolution of this bug, see the previous edition for reference.
2 POSIX is the standard that defines what a Unix system is and how it behaves. This standard is published by The Open Group. See also the question "What exactly is POSIX?".
Technically, the synopsis is correct, but it is confusing. There are cases where no filename is needed:
rm --help
rm --version
(when using GNU rm
). All other cases require a filename.
Other versions of rm
show the file as non-optional, e.g. in the OpenBSD manpage.
A more accurate synopsis for GNU rm
would show the three variants:
rm
[options...] file...
rm --help
rm --version