6

In bash, I am not sure if my observation is correct. For most commands, option-like arguments, i.e. arguments that begin with --abc or -a, can be anywhere in the list of arguments to the command, while other arguments have some fixed position in the list of arguments.

I was wondering if there are some written/unwritten rules about this? Thanks!

4
  • Anyone recalls a command where argument order matters? Because I don't.
    – alex
    Jul 21, 2011 at 17:09
  • 1
    I can - piping stuff into grep xD oh, and rsync, and ln Jul 21, 2011 at 17:57
  • @alex: I can think of dozens. cp, mv, rsync, ssh, watch, make, find ... the list goes on.
    – Caleb
    Jul 21, 2011 at 20:25
  • Okay, okay, what if restrict that to options, not arguments: i.e. -a, -b, -c, etc.
    – alex
    Jul 21, 2011 at 20:42

3 Answers 3

9

In short - it depends on the program. What bash does is it sends the arguments to the program's main function, which from then on decides what to do with them. Some programs don't care about argument order, some take it into account.

2
  • 1
    Although usually if the program takes -aa arguments, it's a strong indication that it's using getopt() or similar, which doesn't care about arg order
    – Shadur
    Jul 21, 2011 at 14:40
  • 2
    @Shadur getopt preserves argument order; it's up to the application whether the argument order matters. Jul 21, 2011 at 15:28
2

Here's a notable example where a program's arguments' order matters: Compare output from:

find . -type f -atime 0 -daystart

with:

find . -type f -daystart -atime 0

(On a drive that stores a file's atime, I need to add these days). There are many webpages devoted to getting you started with find(1), but almost none mention the fact that find, for one, is VERY particular about the order of its arguments.

1

man commandName Will tell you if it matters for the command you are trying to use, generally, some thing which starts with - are options which modify the behavior of the command, and the others are arguments (you can take it as input) for the command.

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.