5

According to several sources, the UNIX utility guidelines specify that operands should always be processed after options:

utility_name[OPTIONS][operands...]

Some older UNIX utilities are known to not follow these conventions quite so, e.g, find, but newer and well-established utilities do too break the rules without an apparent explanation, e.g, curl <url>.

I would like to know if there is a good reason for this and what is the community general consensus on this.

3

The normal convention is that arguments always follow options. The first non-option (the first string on the command line that does not start with -) terminates the options and begins the arguments.

Some tools, notably the build tools (compilers, linkers), have always gone against this convention. Another example that you note is find. Sometimes this is done because the options take effect at the point on the command line where they appear, so you need a way to specify arguments both before and after the option, where the option applies to that argument only if the argument appears after the option.

This convention allows you to write a shell script that contains a line like this:

rm foobar ${more_things_to_remove}

...and guarantee that you can't accidentally add options to the rm command even if the shell variable more_things_to_remove has a nasty value like "-rf".

That convention predates the more recent convention of using the special option -- to terminate option processing. -- is a much better way of marking the end of options explicitly:

rm -- foobar ${more_things_to_remove}

# and it works even if you don't need to delete something called "foobar":
rm -- ${more_things_to_remove}

So lately (and by lately, I mean this has already been doing on for many, many years) lots more command line parsers appear to have been moving toward breaking the earlier convention and allowing options and arguments to be mixed apparently everywhere (subject always to -- forcing the end of options) even if they don't have any special reason to break the convention like compilers and some other tools did.

Personally I never know which utilities still adhere to the convention and which don't, so I always place options before arguments as before, and I am mildly surprised when I see someone else's working code which does it in the opposite order!

  • 2
    Actually, find does want options to come before operand, but confuses the matter by having operands that start with - (primaries like -name, -depth, -print, etc. are not classified as options, only options like -L). – Gilles Feb 9 '15 at 16:22
  • Quite right, @Gilles. – Celada Feb 10 '15 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.