Friday, May 3, 2024
129
rated 0 times [  129] [ 0]  / answers: 1 / hits: 139089  / 1 Year ago, fri, february 24, 2023, 9:47:42

While executing a C program, a.out, using the Ubuntu terminal, why do I always need to type ./ before a.out, instead of just writing a.out? Is there solution for this?


More From » command-line

 Answers
3

When you type the name of a program such as a.out the system looks for the file in your PATH. On my system, PATH is set to



/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games


Yours is probably similar. To check, enter echo $PATH in a terminal.



The system looks through these directories in the order given and if it can't find the program produces a command not found error.



Prepending the command with ./ effectively says "forget about the PATH, I want you to look only in the current directory".



Similarly you can tell the system to look in only another specific location by prepending the command with a relative or absolute path such as:



../ means in the parent directory eg ../hello look for hello in the parent directory.



./Debug/hello : "look for hello in the Debug subdirectory of my current directory."



or /bin/ls : "look for ls in the directory /bin"



By default, the current directory is not in the path because it's considered a security risk. See Why is . not in the path by default? on Superuser for why.



It's possible to add the current directory to your PATH, but for the reasons given in the linked question, I would not recommend it.


[#30309] Saturday, February 25, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pardsea

Total Points: 290
Total Questions: 115
Total Answers: 98

Location: Svalbard and Jan Mayen
Member since Sun, Sep 25, 2022
2 Years ago
;