Reputation: 1571
When I do something like:
./foo -uxw --bar something
Does the shell automatically parse these commands, or does each program have to do the parsing itself?
Upvotes: 4
Views: 501
Reputation: 44424
Program arguments are not the concern of the shell.
The (simplified) command-line syntax is: command options operands.
Options begin with a hyphen (dash) and operands do not, options are not ordered, operands are ordered.
This though is only a convention, not a law. The most well known standard for this is generally referred to as the POSIX standard, and it defines the command-line here.
You may note that options are single characters prefixed with a single hyphen, your example shows two hyphens with multiple-characters --bar
. These are known as long options and are not strictly part of the standard, although they are commonly used in GNU utility programs (the normal case on Linux).
What happens if an operand begins with a hyphen? The answer is that most programs will see that as an operand. There is a special marker though; --
(two hyphens) which mark the end of the option list, so any that follow are only operands.
Upvotes: 0
Reputation: 34294
No, the shell doesn't parse it for you. Each program has to parse it on its own. The following code should make it clear what is going on.
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
printf("argc: %d\n", argc);
for (i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}
Let us compile this program.
susam@nifty:~$ gcc args.c -o args
Now let us run this and see the output:
.susam@nifty:~$ ./args
argc: 1
argv[0] = ./args
susam@nifty:~$ ./args foo bar
argc: 3
argv[0] = ./args
argv[1] = foo
argv[2] = bar
susam@nifty:~$ ./args -a foo --b bar
argc: 5
argv[0] = ./args
argv[1] = -a
argv[2] = foo
argv[3] = --b
argv[4] = bar
The only thing the shell does is pass each argument you specify in the command line to your program. While it would pass foo bar
as two separate arguments to your program, it would pass "foo bar"
or 'foo bar
' as a single argument to your program. Yes, so the shell does some sort of parsing of the arguments before passing it to your program. It considers quoted strings as a single argument. Here is a demonstration:
susam@nifty:~$ ./args -a foo bar
argc: 4
argv[0] = ./args
argv[1] = -a
argv[2] = foo
argv[3] = bar
susam@nifty:~$ ./args -a "foo bar"
argc: 3
argv[0] = ./args
argv[1] = -a
argv[2] = foo bar
susam@nifty:~$ ./args -a 'foo bar'
argc: 3
argv[0] = ./args
argv[1] = -a
argv[2] = foo bar
susam@nifty:~$ ./args -a "foo bar" 'car tar war'
argc: 4
argv[0] = ./args
argv[1] = -a
argv[2] = foo bar
argv[3] = car tar war
Upvotes: 2
Reputation: 157484
The shell splits the command line on whitespace, so the program receives a list of arguments; but it's up to the program to decide what they mean. Often an argument parsing library like getopt
is used to help.
Upvotes: 1
Reputation: 182734
Each program parses its arguments. You'll probably want to look into getopt
for that so the answer becomes: each program usually relies on getopt
to parse arguments.
Upvotes: 7
Reputation:
Each program has to parse all arguments itself. Prefixing them with dashes is just a Unix convention.
Upvotes: 1