Sample Header Ad - 728x90

What is the reasoning behind accepting truncated long command line options?

1 vote
2 answers
121 views
Many command line utilities accept long ("GNU style", according to ) options such as
--version
. To my surprise, truncated versions are often interpreted as the full option. For example,
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 .
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
or
(or similar), so I have looked into their manpages for clues, but not found any explanation. E. S. Raymond. *The Art of UNIX Programming*. Pearson Education, Inc, 2004
Asked by Mårten W (119 rep)
Nov 12, 2021, 11:14 PM
Last activity: Nov 13, 2021, 12:04 AM