Why "./myfile" instead of just "myfile"? (to run executable)
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.
Why "./myfile" instead of just "myfile"? (to run executable)
I am often creating new executables as I am brushing up on my C++, but it is annoying to always have to type "./executable" instead of just "executable" to run the newly compiled program.
Is there any way to change your new executables so that you don't have to type './' in front of it all the time? (I could modify makefile to do this after compiling)
Thanks in advance...
P.S. I use Fedora core 4 if that makes any difference
The ./ (. is the current directory and / is the path separator) refers to the current PATH. Unlike Windows, in *nix systems the current directory is not part of the executable search $PATH in the shell, so it must be specified explicitly. You can add . to the end of your PATH (search around here for details -- it's been discussed many times), but you shouldn't do this to the root user for security reasons, and certainly don't put . anywhere in your $PATH but last, otherwise someone could create a malicious file with the same name as a common binary and trick you into running it.
You can put executables in a directory which is in PATH, or you can add a directory to PATH. If you are doing a lot of this, then just make a special directory which is in PATH.
eg:
in HOME: mkdir bin
export PATH="$PATH:~/bin"
to make this permanent, you may also need to edit your startup files
I first started to learn unix command line as a required class in college (Introduction to Unix/Linux). Our professor was an "aged" man who basically started using Unix when it first became popular in the early 70s. He worked as a network administrator until about 8 years ago when he decided to start teaching... and is a consultant to the school's network. When we were learning the UNIX environment, we went over the whole search path thing and executables. As part of our exercises, we made a bin directory in our home folder and all our scripts were to be put there and nowhere else. What makes me smile about this thread is one of my classmates simply asked the question about why not put the current directory in the search path rather than always use ./ or putting the script in "that bin directory." This spawned a 20 minute lecture about all the potential security risks that could arise from doing something like that. One thing he mentioned (which I might not explain perfectly) is that if someone wanted to install malware or rewrite a command and make it do something malicious, it would be so much easier if the current directory were in the search path. We also got the bonus story of how he had to fire an underling for consistently using the current directory in the search path. Anyway... it all boiled down to the idea that if something is supposed to be executable, then it should be in a directory dedicated to executables so the administrator can track them. If you're in the habit of making executables, then by all means it's just damn safe to make a ~/bin directory and add it to your search path. When I first got Slackware and experimented with it, I was a little taken with the current directory being in my search path. It was one of the first things I got rid of because I'm so deep in the habit of using ~/bin. Sorry if my story bored you... but I figured I would share my experience and endorse the ~/bin option.
It's a deliberate step intended to make sure that, if you find yourself executing a program that's in your current-directory, then you know that you are doing so. Otherwise, someone could "slip" a rogue program into your current directory, masquerading as a commonly-used command.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.