I am wondering how I can add linux command option at the end without error.
For example
rm file1/ -r
cp file1/ file2/ -r
I experience some Linux cluster system can do it, bus others cannot.
As searched for while, getopts command may help but I am not sure if getopts is best choice for that and (also not sure how to implement for that my purpose).
Do I need to customize command by command or there is more general solution that can be applied any command?
Thank you for your help.
Advertisement
Answer
Consider this command:
rm -f myfile -r -- -i
The GNU flag convention is to allow options anywhere, up until an optional --
to indicate “end of options”. Programs following it will see the options -r
and -f
, plus the arguments myfile
and -i
The BSD flag convention is that flags are only allowed up until the first non-flag argument, or until an optional --
. Programs following it will see the option -f
, plus the arguments myfile
, -r
, --
and -i
.
POSIX only requires utilities to support the BSD style.
It’s up to the individual program to decide how to interpret flags. If you’re on a BSD style system like FreeBSD or macOS, you can install GNU tools and use those. If you can’t, you’re mostly stuck with the system’s flag convention.