Many command line utilities accept long ("GNU style", according to [1]) options such as --version
. To my surprise, truncated versions are often interpreted as the full option. For example, df
from GNU Coreutils gives
user@computer ~ $ df --version
df (GNU coreutils) 8.32
Packaged by Gentoo (8.32-r1 (p0))
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by Torbjorn Granlund, David MacKenzie, and Paul Eggert.
The same output is obtained by replacing --version
with any of the truncated variants --v
, --ve
, --ver
, --vers
, --versi
and --versio
. However, when I add additional letters to the option, it suddenly realises that the option is invalid:
user@computer ~ $ df --versionn
df: unrecognized option '--versionn'
Try 'df --help' for more information.
Is this behaviour intentional? What is the reasoning is behind it?
My understanding is that most utilities rely on getopts
or getopt
(or similar), so I have looked into their manpages for clues, but not found any explanation.
[1] E. S. Raymond. The Art of UNIX Programming. Pearson Education, Inc, 2004
--v
, there is only 1 match, among the accepted options.--versionn
is a different string entirely, and matches none of the accepted options. If you're curious, you could get the source fordf
and read it.df
since it usesgetopt_long
, but in any case, this does not tell me anything about the reasoning or design choice behind the behaviour.man getopt
andman 3 getopt
, but notman getopts
, which was the one I studied most closely.