Invalid command line argument number when passing `*`

I am writing a program in the language "Reverse Polish notation", which takes its operands and operators through command line arguments. But everything happens wrong when the "*" operator multiplies, and I don’t know why.
Here is a small debugging program.

test.c

int main(int argc, char **argv) { printf("%d\n", argc); return 0; } // run case result ./test ab 3 ./test * 66 

So why does the argument β€œ * ” make the wrong result?

+6
source share
2 answers

* performs a global shell. Thus, it will apply to all files in your current directory, which will be the arguments of your program, you have 65 files in your directory. You can see what happens if you run echo *

You need a single quote * , as in ./test '*' (double quotes will work too), this will prevent shell expansion * . A * provided to your program in this case, the shell removes single quotes.

If you want to evaluate expressions, you can do

 ./test 3 2 '*' 

In this case, your program receives 3 additional arguments, separated as follows: argv[1] is 3 , argv[2] is 2 and argv[3] is *

Or you could do:

 ./test '3 2 *' 

In this case, your program receives 1 additional argument, argv[1] will be line 3 2 *

+11
source

Your command shell treats * as a template. It probably includes all the files in the current directory: 60ish in your case.

+8
source

All Articles