Question

I have this very simple c program:

#include <stdio.h>
int main (int argc, char ** argv){
  printf ("%s\n",argv[1]);
}

When running it on Linux/bash like so:

./a.out *

I get the following output:

a.c

why?

Was it helpful?

Solution 2

I didn't know that, but when running a command line that has a glob character , such as * or ?, the command line interpreter first expands the character and only then run the program.

For example, if your program is:

#include <stdio.h>
int main (int argc, char ** argv){
  int i;
  printf ("argc=%d\n",argc);
  for (i=0;i<argc;i++){
    printf("%d: %s\n",i,argv[i]);
  }
}

and you run it like so:

./a.out *

, then the output will be:

argc=4
0: ./a.out
1: a.c
2: a.c~
3: a.out

Of course, the output will depend on the content of the current directory.

OTHER TIPS

Because * is a glob character that expands to the list of files in the current directory.

If you want to pass a literal * you will need to quote or escape it:

./a.out '*'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top