Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I was teaching my cousin how to code, and I looked stupid in front of her when I couldn't get a simple Hello World program to output to the command line.
The code is right, and gcc compiled no problems. I spent 15 minutes trying to get it to work. I was trying to run
Code:
test
without the "./" before it. The problem is that there were no errors and no warnings. It seemed like my code just had a run time error, which didn't make sense because it was this:
Code:
#include <stdio.h>
int main() {
printf("Hello World");
return 0;}
to actually get it to work I have to run
Code:
./test
I know I can add that to my ~/bin and then I don't need to type in the "./"
My question is: WHY is "./" necessary? And why does running the file without "./" cause no errors yet does not run correctly?
Thank you very much
Last edited by /dev/dog; 01-02-2015 at 08:46 AM.
Reason: "#include <stdio.h>"
to see where it is. Usually /usr/bin/test. Without arguments, it simply does nothing and returns with error code. Path priority is so that this program is found before your program, or your program is not found at all.
Last edited by cepheus11; 01-02-2015 at 09:16 AM.
Reason: typo
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
To put it briefly the path environmental variable tells Linux where to look for executables. This path does not include the current working directory by default (as it does in, say, Windows) because this could be a security risk or simply cause the wrong thing to be executed. Suppose, of you will, you accidentally create an executable called "cd", "rm", "mv" or similar and then try to execute those commands in the diretory in which executable resides. Now suppose that you regularly use directories which other, whom you may not trust completely, are able to write to.
It could, perhaps, be argues that this is less important nowadays but, as with a lot of these security related "quirks" on Linux it is part of Linux's security as a whole.
to see where it is. Usually /usr/bin/test. Without arguments, it simply does nothing and returns with error code. Path priority is so that this program is found before your program, or your program is not found at all.
Amazing! I did not know that, that is very clarifying! It did not even cross my mind that test is a program. And thank you 273 for explaining that my ~/bin/test would be found last. Yes, test resides /usr/bin/test.
After renaming my executable to testt, and setting my Makefile to place it in ~/bin/testt, it works without having to specify the path.
Distribution: Debian Sid AMD64, Raspbian Wheezy, various VMs
Posts: 7,680
Rep:
Glad to help, I meant to mention that you can change the order in which Linux searches by changing the order in the path environmental variable but I would't recommend that, for the reasons mentioned in my previous post regarding security, unless you research and know exactly what you're doing.
Amazing! I did not know that, that is very clarifying! It did not even cross my mind that test is a program.
It's even worse than that. The test command is used so frequently that it is also built into most shells, as you will see if you run "type test". That means that no matter how you set up the search sequence in your PATH variable, the shell will never run either your program or the one stored in /bin or /usr/bin.
You learn quickly that when you are trying to write a program, do not call it "test". (Personally, I use "try".)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.