Meri The'Badger Meri The'Badger - 2 years ago 117
Bash Question

Is there any established order for 'ls' arguments?

I'm a student and as part of my circus I must recode the

command and reproduce its behavior the best possible.

On Mac (El Capitan 10.11.6), using the terminal iTerm 2(zsh), I get:

user> ls . -R
ls: -R: No such file or directory

And on Arch (latest version), using the default text interface(bash) I get:

user> ls . -R
<current directory content>

Although I'd rather trust Arch, is it correct for
to refuse its option after a directory has been specified? And is there any documentation declaring the arguments order?

Answer Source

See POSIX utility syntax guidelines, entry #9:

Guideline 9:

All options should precede operands on the command line.

Thus, the usage that POSIX guarantees will be supported is ls -R ., as -R is an option, and . is an operand.

GNU tools don't generally enforce this guideline (which is why Arch lets you put your arguments in the opposite order) -- but for maximum portability it's wise to write your software assuming it to be enforced.

If you want to be certain that -R will be treated as an operand rather than an argument, whatever your platform, you can use -- to separate the two categories: ls -l -- -R will treat -l as an option and -R as an operand, and thus will look for a file or directory named -R and provide a long-format listing; this is defined by POSIX utility syntax guideline #10.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download