Is there anything special about command line arguments prefixed with dashes? Is there anything special about command line arguments prefixed with dashes? shell shell

Is there anything special about command line arguments prefixed with dashes?


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.


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:~$ ./argsargc: 1argv[0] = ./argssusam@nifty:~$ ./args foo barargc: 3argv[0] = ./argsargv[1] = fooargv[2] = barsusam@nifty:~$ ./args -a foo --b barargc: 5argv[0] = ./argsargv[1] = -aargv[2] = fooargv[3] = --bargv[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 barargc: 4argv[0] = ./argsargv[1] = -aargv[2] = fooargv[3] = barsusam@nifty:~$ ./args -a "foo bar"argc: 3argv[0] = ./argsargv[1] = -aargv[2] = foo barsusam@nifty:~$ ./args -a 'foo bar'argc: 3argv[0] = ./argsargv[1] = -aargv[2] = foo barsusam@nifty:~$ ./args -a "foo bar" 'car tar war'argc: 4argv[0] = ./argsargv[1] = -aargv[2] = foo barargv[3] = car tar war


Each program has to parse all arguments itself. Prefixing them with dashes is just a Unix convention.