Is there anything special about command line arguments with a dash prefix?

When I do something like:

./foo -uxw --bar something 

Is the shell automatically parsing these commands, or should each program independently perform parsing?

+4
source share
6 answers

No, the shell does not analyze it for you. Each program should analyze it independently. 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; } 

Compile this program.

 susam@nifty :~$ gcc args.c -o args 

Now let's 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 on the command line to your program. Although it would pass foo bar as two separate arguments to your program, it would pass "foo bar" or 'foo bar ' as one argument to your program. Yes, so the shell does some sort of parsing of the arguments before passing it to your program. It treats quoted strings as one argument. Here is a demo:

 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 
+2
source

Each program analyzes its arguments. You probably want to study getopt to get an answer: each program usually uses getopt to parse the arguments.

+7
source

Each program should independently analyze all the arguments. The prefix of their dash is just a Unix convention.

+1
source

No, the shell does not analyze these parameters. Each program should independently analyze it.

+1
source

The shell breaks the command line into spaces, so the program receives a list of arguments; but it depends on the program to decide what they mean. Often used getopt syntax reference library.

+1
source

Program arguments are not shell-specific.

Command line syntax (simplified): operands of command parameters.
Options start with a hyphen (dash) and there are no operands, parameters are not ordered, operands are ordered.

This, although it is only a convention, not a law. The best known standard for this is commonly referred to as the POSIX standard, and it is defined here .

You may notice that the parameters are single characters with prefixes with one hyphen, in your example two hyphens with several characters --bar . They are known as long variants and are not strictly part of the standard, although they are commonly used in GNU programs (normal case for Linux).

What happens if the operand starts with a hyphen? The answer is that most programs will see this as an operand. However, there is a special marker; -- (two hyphens) that mark the end of the options list, so any subsequent ones are only operands.

0
source

All Articles