+5  A: 

You should add the directory where your compiled program is to your PATH.

For example if you are inside the /home/jimmy/cpp directory

type (leading $ is the prompt)

PATH=$PATH:`pwd`
$myprog

Read about exporting variables and the bashrc file to make this change permanent. (assuming bash as your shell)

Tom
Adding '.' to your path is an extremely, extremely bad idea.
anthony
There, i switched it to`pwd`
Tom
Much more likely you should copy the program to $HOME/bin and ensure $HOME/bin is on your PATH.
Jonathan Leffler
@Anthony: I'm not a shell guy.. why exactly is that a bad idea?
Billy ONeal
@Billy ONeal: echo "rm -rf /" > ls. or, more generally, because typing a command will do different (unknown, possibly bad) things depending on $PWD.
anthony
@Billy: another way of explaining - if you type something it searches your path in order. If . is early in the path, then someone who anticipates that can put a bogus command with the same name in a directory you might cd into and that they can write to (e.g. /tmp), allowing them to do arbitrary things under your user id. Similarly, they may anticipate possible typos, like 'sl' for 'ls', which would give them control even if '.' is the last directory searched. The command's output can look as if the real command ran, or 'sl not found'/exit(1) as appropriate.
Tony
@Billy: See my detailed answer in this question.
abelenky
+16  A: 

You must learn the ways of the PATH if you're to come with me to Alderaan.

anthony
That should've been: "Yoder, you must learn the ways of the PATH". :)
abelenky
Never his mind on what directory he was in. Hmm? What process he is running. Hmph.
anthony
Mudhole? Slimy? My $HOME this is!
anthony
I have altered the PATH. Pray I do not alter it further.
Fred Larson
moving on to jedi now: Strong am I with the PATH, but not that strong.
anthony
too easy: Once you start down the dark PATH, forever will it dominate your destiny
anthony
Nominated for Comment Thread of the Year!
John Dibling
+2  A: 

Personally, I use $HOME/bin as a personal collection point for utilities I wrote but that won't be of use to all users. Otherwise, /usr/local/bin is often the right place for locally written programs that are of use to all users. In any case, I have verified that the latter place was on my path and added $HOME/bin as well.

Unless you really are an installer, it is probably not a good idea to go dropping programs into /bin or /usr/bin despite temptation. That would be the moral equivalent of putting your programs in C:\Windows or C:\Windows\System32. It is effectively never the correct answer.

Learning more about the PATH environment variable and how the shell searches for programs to run is definitely recommended. Also, as pointed out in another comment, just don't add . to your PATH because it will come back to haunt you at some point.

Edit: Incidentally, I do a very similar thing on my Windows boxen. I always create a sibling to C:\Program Files named C:\Programs. In there, I make a folder named bin and add that to the system path. I use C:\programs\bin much like I use $HOME/bin on a *nix box, and put any hand-installed (i.e. no real Windows installer was used) programs or stuff ported from *nix that can't tolerate spaces in its path in there in other folders such as C:\Programs\mingw, C:\programs\MSYS, or C:\programs\cygwin.

Any small utilities primarily used from the command prompt usually end up in C:\programs\bin, but for anything I'm seriously planning to use on more than one PC I generally create a real Windows installer with InnoSetup and let it get installed in C:\Program Files.

RBerteig
If a program cannot tolerate spaces in it's path, than it is an extremely poorly written program. Unix boxes have had paths with spaces in them far longer than Windows has.
Billy ONeal
True, but there has been a strong cultural aversion to actually using spaces in file names. The shell's splitting of its command line on whitespace is part of the problem. Similar issues exist on Windows, but the command prompt has been such a small part of the typical user's experience that white space in names is effectively encouraged. The command prompt quoting problems are no smaller, however.
RBerteig
+2  A: 

http://ss64.com/bash/alias.html

e.g.

alias progname=/path/to/program/progname
Ben Voigt
+4  A: 

A detailed discussion of why putting . (current directory) in the PATH is a bad idea.

Lets say you're being attacked by an adversary on your machine. He authors a malicious program, and puts it in a directory, hoping that you'll stumble on it eventually. To increase his chances, he names it something common like mv.

If you have added . to the beginning of your path, and happen to be in the right directory when you type mv onefile twofile... then the local mv (./mv) gets run instead of the mv command we're all used too! This happens because . is in your path, and the local mv will be found before the /usr/bin/mv. Suddenly, your user account or the entire machine may be horribly compromised.

(note: mv might be one of the built-in commands, and immune to this. Not sure... but the principle is solid)

So, you learn the lesson, and now put . at the end of your path, so that all "official" directories will be searched before the local directory.

But the attacker is now on to you! Instead of the program mv, he creates in a program mc, which is a common typo for mv. Again, you intend to type mv onefile twofile, but make a simple typo to mc. Now all the "official" directories are searched, the program mc is not found, and finally it is found in the local directory. Malicious code is run, and again you lose.

The lesson is that your PATH should only cover known-good software, and since your current directory changes frequently, you never know exactly what software is there, and should never run it unless you're very explicit about it with the ./ prefix (eg. > ./IMeanToRunThis)

abelenky
This is an excellent description despite the lack of star wars quotes
anthony
+1 .. but one more question ... Hmm.. if someone is able to drop an executable into anything other than their home directory, have they not already compromised the machine?
Billy ONeal
Nope... there are frequently many shared directories on a machine, consider such common names as /tmp, /public/ftp/uploads. And a sys-admin may still end up in a users-home directory from time to time.
abelenky