2005/04/27 PATH, command

Your PATH (echo $PATH) is where your shell will search for executables. If a command you want to run isn't included in $PATH, you have to be specific: /somewhere/someprog. One confusion for Unix/Linux newcomers is that if PATH doesn't include "." (current directory), then trying to run a program you can see right in front of your nose with "ls" is going to generate a "command not found" message from your shell. Unlike MSDOS, Unix shells don't usually assume they should look in the current directory without being told to. Remember, this is a function of the shell you are using and NOT Unix/Linux itself. Someone COULD write a shell that automatically looked in the current directory for commands - but they aren't likely to.

Why not? Because it's a bad idea. It's especially a bad idea for root because of the very real possibility of being tricked into executing the wrong program with (potentially) disastrous consequences. It's true that your shell searches through PATH in order, so appending the "." to the end is less dangerous than sticking it elsewhere, but simple fat-fingered typing can catch you even with that. Especially for root, it's best not to do that and just learm to type "./this" when you really do want to rn the "this" in the current directory.

It's definitely a bad idea for root, and probably not a real good idea for anyone. But it does raise a question in my mind - why would you want "." in your PATH anyway? Yes, good old Microsoft stuff defaults to that behaviour, but that shouldn't justify defaulting non-root users to include that.. and does any system still do that? I'm not sure I've seen one recently.. oops, yes I have: SCO systems still set this by default (at least as of OpenServer 5.0.7) :


At least it's at the end..

but other than that, I don't think I've seen "." added by default anywhere. So what does determine your PATH? A default PATH is probably compiled into your shell, but it's usually reset or at least added to by startup files. In the case of bash, that would be /etc/profile, /etc/bashrc and your own "dot" files. PATH should be set in .bashrc (but see ./bashrc doesn't run automatically, .bashrc broken and Shell Bashing) if you are changing or adding to it.

The first place PATH is set is by login. As I mentioned, your shell probably has a default compiled in also; it looks like this on Bash 3.0:


Also defined is a failsafe PATH which you can get with "command -p" :

* The value for PATH when invoking `command -p'.  This is only used when
   the Posix.2 confstr () function, or CS_PATH define are not present. */

To see how that works, create a little script that just has "echo $PATH" in it. I called mine "p":

$ cat p
echo $PATH
$ ./p
$ bash ./p
$ command ./p
$ command -p ./p

Notice that isn't what the defaults say it would be, so something set that elsewhere during the compile. "command" is useful even without -p because it does ignore aliases and functions, giving more assurance that you are running the command you think you are.

Got something to add? Send me email.

(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Understanding $PATH, Unix/Linux

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Kerio Samepage

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us