-   Linux - Newbie (
-   -   Why "./myfile" instead of just "myfile"? (to run executable) (

rusty_turkey 06-11-2006 11:20 PM

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

btmiller 06-11-2006 11:32 PM

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.

pixellany 06-12-2006 12:01 AM

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.

in HOME: mkdir bin
export PATH="$PATH:~/bin"

to make this permanent, you may also need to edit your startup files

rusty_turkey 06-12-2006 12:22 AM

aahh! That makes a lot of sense... I can handle that.

Thanks guys!

zetabill 06-12-2006 02:22 AM

This thread brought back memories... :)

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.

sundialsvcs 06-12-2006 03:05 PM

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.

Anyway, "it was done on-purpose."

LzW-x 06-12-2006 04:00 PM

Have you ever typed the set command in a terminal window? That's some pretty crazy stuff right there!

rusty_turkey 06-12-2006 08:17 PM

Thanks Zetabill - it's kinda funny that a guy got fired for adding the current directory to the search path though... (was probably a windows-user ;) )

I think I'll take ur advice and make a ~/bin directory, and my makefiles can simply chuck the executable in there.

Thanks again all!

Now I know more about that wonderful OS that is Linux; mmm... feel the power...

All times are GMT -5. The time now is 01:32 PM.