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) :
PATH=$PATH:$HOME/bin:.
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:
#ifndef DEFAULT_PATH_VALUE #define DEFAULT_PATH_VALUE \ "/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:." #endif
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. */ #ifndef STANDARD_UTILS_PATH #define STANDARD_UTILS_PATH \ "/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc" #endif
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 /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pcunixapl/bin $ bash ./p /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pcunixapl/bin $ command ./p /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pcunixapl/bin $ command -p ./p /bin:/usr/bin $
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.
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. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar