Evolution of shells in Linux (developerWorks)
Evolution of shells in Linux (developerWorks)
Posted Dec 9, 2011 8:52 UTC (Fri) by HelloWorld (guest, #56129)In reply to: Evolution of shells in Linux (developerWorks) by ekj
Parent article: Evolution of shells in Linux (developerWorks)
Well yes, if you actually want to specifically list a file called -l, you need to type something else than -l, such as ./-l. But that is the simple, obvious case that will be caught when you first try to run your script. It's another thing if a filename such as -l is generated by globbing. The issue here is that an application should know whether an argument was generated by globbing or not, so that it can treat an argument such as -l as a positional parameter instead of an option if it was generated by a glob pattern. But it can't, since, globbing is done by the shell, and the information about which parameters were globbed and which weren't is lost.
Posted Dec 9, 2011 12:19 UTC (Fri)
by nix (subscriber, #2304)
[Link] (1 responses)
Posted Dec 9, 2011 12:38 UTC (Fri)
by HelloWorld (guest, #56129)
[Link]
Posted Dec 9, 2011 16:30 UTC (Fri)
by nybble41 (subscriber, #55106)
[Link]
There's a standard solution to this: instead of "ls *.c", write "ls ./*.c", which has the same effect, and yet has no chance of accidentally expanding to an option rather than the expected filename.
Or, for any program which has a standard getopt-style command-line parser, just use "--" before any glob patterns.
Evolution of shells in Linux (developerWorks)
That's rather interesting, actually. I guess it was removed because there are corner cases left. For example, something like
Evolution of shells in Linux (developerWorks)
touch -- --harmful-flag
foo=(*)
foobar "${foo[@]}"
would likely still not be caught. Of course, it would be possible to treat variable expansions as positional arguments as well, but that would probably break lots of scripts. Incrementally building a list of flags in a shell variable is a common idiom, after all.
Evolution of shells in Linux (developerWorks)