I've been going over this question and I can't for the life of me figure out why the answer is what it is. How many arguments are passed to the command by the shell on this command line:
<pig pig -x " " -z -r" " >pig pig pig
a. 8 b. 6 c. 5 d. 7 e. 9
The first symbol is supposed to be the symbol for redirected input but the site isn't letting me use it. [Fixed.]
I looked at this question and said ok...arguments...not options so 2nd pig, then " ", then -r" ", 4th pig and 5th pig...-z and -x are options, so I count 5. The answer is b. 6. Where is the 6th argument that's being passed on?

  • Ok thanks guys...discovered that the prof is throwing in made up commands and programs we've never heard of to throw us off on the midterm. He has never once mentioned that there is anything in unix called pig...and if you try to man pig in the bash shell...you get nothing back so I assumed it was another argument and the command was arbitrarily to the left of redirected input. – Ryan Brown Oct 26 '12 at 12:22
  • He's not trying to throw you off--he's trying to drill into your head that it doesn't matter what the command is named or what it does. What matters is the syntax of the command line. The shell doesn't even know if the command exists at the time that it parses the command line into its argument components. All the shell knows is that the first thing on the command line not a redirection or an assignment is the name of the command and that everything following except redirections are arguments. After the shell has parsed the line, then it attempts to find the command to execute it. – garyjohn Oct 26 '12 at 15:41

I think you're running into semantics issues with exactly what counts as an argument.

Options are, at the command line level, arguments that the command happens to treat specially. (More specifically: they are included in the argv array passed to the command.) So depending on exactly what's meant by "argument", they might or might not be included in the count. I suspect they are being included, but that the second "pig" isn't (because it's the commandname, which isn't an argument) (well, ok, it's included in argv too, but it doesn't count) (mostly).

To illustrate what I mean about options being specially-treated arguments (and the arbitrariness of the distinction), consider the following command:

tar cv -f foo.tar bar
  • tar is the command
  • cv is two options (create and verbose). I know it doesn't look like it, but tar predates consistent use of - to introduce options, and will treat its first "argument" as one or more options even if it doesn't start with -
  • -f foo.tar is another option and its argument (which is different from a general command argument)
  • bar is a normal command argument (finally!)

Note that these commands are both equivalent to the above:

tar cvf foo.tar bar
tar cv -ffoo.tar bar

The arguments passed to the pig program are:

  1. -x
  2. " "
  3. -z
  4. -r" "
  5. second-to-last pig
  6. last pig

From a programming perspective, it's customary to consider everything in argv (except argv[0]) an argument to the program. This includes "option" tokens -- I mean, what if the program didn't take any options at all, and instead considered something like "-x" to be a filename?

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.