Section 5.19. Finding a Command:


[Page 194 (continued)]

5.19. Finding a Command: $PATH

When a shell processes a command, it first checks to see whether it's a built-in; if it is, the shell executes it directly. echo is an example of a built-in shell command:

$ echo some commands are executed directly by the shell some commands are executed directly by the shell $ _ 


If it isn't a built-in command, the shell looks to see if the command begins with a / character. If it does, it assumes that the first token is the absolute pathname of a command, and tries to execute the file with the stated name. If the file doesn't exist or isn't an executable, an error occurs:

$ /bin/ls           ...full pathname of the ls utility. script.csh  script.ksh $ /bin/nsx          ...a nonexistent filename. bash: /bin/nsx: not found $ /etc/passwd       ...the name of the password file. bash: /etc/passwd: Permission denied     ...it's not executable. $ _ 


If it isn't a built-in command or a full pathname, the shell searches the directories whose names are stored in the PATH environment variable. Each directory in the PATH variable (from left to right) is searched for an executable matching the command name. If a match is found, the file is executed. If a match isn't found in any of the directories, or the file that matches is not executable, an error occurs. The contents of the PATH variable may be changed using the methods described in later chapters, thereby allowing you to tailor the search path to your needs. The original search path is usually initialized by the shell's startup file, and typically includes all of the standard Linux directories that contain executable utilities. Here are some examples:


[Page 195]

$ echo $PATH /bin:/usr/bin:/usr/local/bin    ...directories searched. $ ls                            ...located in "/bin". script.csh  script.ksh $ nsx                           ...not located anywhere. bash: nsx: not found $ _ 


An important note about the $PATH variable: Traditionally, UNIX shells included your current directory (".") in the search path, usually at the end. This is convenient for running shell scripts and compiled programs that you are working on. When you include "." in your $PATH, you can type "a.out" to run a compiled C program rather than having to specify "./a.out" (which explicitly tells the shell that a.out is in the current directory). This may seem trivial, but over the years, having "." in your search path has come to be viewed as a security risk. Someone could plant a program called (for example) ls in a directory that did something nefarious. Then they could try to trick you to going to that directory, and when you typed "ls" then their program might run instead.

Default shell startup files on Linux systems do not include the current directory in the path. Therefore, it is necessary to reference any file you are executing (e.g., a compiled C binary or a shell script) relative to the current directory (i.e., "./test.ksh"). Some of the examples in this book may not always do this, in which case it is presumed that the current directory (".") is included in the search path, even though this is not recommended for most users.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net