Quote:
That is because bash is only so smart. When it sees a hyphen, it assumes that what follows is an option to the command (think of ls -l).
|
As I understand it,
bash isn't looking at the command line options at all. It just passes them to
ls, which is responsible for looking at them (usually via getopt(3) or getopt_long(3)). Same with rm or any other command.
Quote:
Now, long options are by definition the last thing that precedes a filename. Once bash discovers a double hyphen it stops interpreting any following single hyphens as options and it takes them literally, as parts of a filename.
|
I'm not sure that's true. I only have ls version 6.9 here under Cygwin, but I can say "ls --all -l" or "ls -l --all" and it produces the same effect. I believe getopt_long(3) will parse its arguments in any order, long or short.
Quote:
It doesn't matter whether your double hyphens are actually accompanied by a long option or whether they are just that, double hyphens without an option.
|
The double hyphen without an option name is a special case that is checked by getopt(3) and causes option parsing to stop. So everything that follows is taken by the program, ls in this case, as "stuff to work on". To quote from the getopt(3) man page, "The special argument "--" forces an end of option-scanning regardless of the scanning mode."
So here's how I think it all works...
Bash parses the command line and splits it into arguments by looking for spaces and tabs (allowing for quoting etc). It knows nothing about hyphened options. It invokes the binary by looking up the first argument in $PATH, and passes all the command line arguments (including the program name) as the argc, argv[] parameter to main(). The main (or an early) function in the program (ls or rm etc) calls getopt_long, specifying all the legal parameters, long and short. Getopt_long parses the argv[] array, accepts long and short arguments in any order until it runs out of arguments, runs out of things that start with "-" or it reaches "--". If anything remains, these are non-option arguments and the program treats them as such.
Most programs use getopt or getopt_long, which is why "--" works with most programs. Bash isn't involved, which is why it works with ksh and any other shell.
HTH,
-nick