Home > front end >  Why argv[0] is ./a.out not a.out
Why argv[0] is ./a.out not a.out

Time:09-02

I wrote simple program that prints out argv[0] and compiled by using gcc in terminal. Then i executed a.out file by writing ./a.out

I expected the outcome to be a.out but it was “./a.out”. Isnt argv[0] is a program name? Or program name includes the path??

Thanks a lot

CodePudding user response:

The first argument (argv[0]) will include the path. You can see this by moving to another directory and executing your simple program from that location. You will need to do your own parsing to get just the program name.

CodePudding user response:

Executables under Linux use special paths like /usr/bin/ to search for executables. As security measure the current directory (.) is not sought through for executing.

But if you explicitly write ./my_own_batch_file.sh then that is executed.

Thus if you are in some directory with thousands of files, you still can type ls (Windows dir) without surprises.

  • Related