home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 8.6 Output Command-Line Arguments Chapter 8
How the Shell Interprets What You Type
Next: 8.8 A Directory for Commands You Shouldn't Run
 

8.7 Setting Your Search Path

Your search path (6.4 , 6.5 ) controls what directories - and in what order - the shell searches for external (1.10 ) commands. You can set a search path that takes effect every time you log in by editing your shell setup file (2.2 ) . You might also want to change the path temporarily.

8.7.1 Setting Path in Shell Setup Files

To change the "default" search path used every time you log in, edit the PATH=... line in your .profile file or the set path=(...) line in your .cshrc or .login file.

Add the absolute pathname (14.2 ) of the directory to the path. You have a choice:

  • You can put the directory at the end of your path. (I think that's the best idea unless you know exactly what you're doing.) Then, commands in the directories you add will be used only if they have unique names that aren't found anywhere else in the path. You can check that with a command like which (50.8 ) .

  • If you put the pathname close to the start of the path, before standard system directories like /bin , then commands in the directory you add will be used instead of system commands with the same name. That lets you replace commands that don't work the way you want with your own version. For instance, if you had the cal script that marks today's date (48.7 ) in your bin (4.2 ) , it would be used instead of the system cal (48.6 ) .

    If you set your path this way, you should be especially careful not to accidentally give some random program the same name as a system command - article 44.21 explains how to check for that. Also, be sure to make the directory unwritable by other users (with chmod go-w )-so they can't add malicious programs with the same names as system utilities.

CAUTION: Installing your own version of standard system commands (like ls or rm ) at the front of your path has a serious consequence. Many system programs and shell scripts will call a program like ls and expect it to work just like the default system version of that program. If you install a version at the front of your search path that behaves differently, that can cause serious problems for an unsuspecting program. For example, you might install a version of rm that writes messages to standard output like "Do you want to remove this file?" and reads your answer from standard input. The standard system rm command won't prompt if its standard input isn't a terminal. If your custom rm doesn't work the same way as the system rm , other programs that call rm can mysteriously lock up while they wait (forever) for your private rm to get an answer to its prompt. If you want to replace a system command, it's better to give your version a different name.

When you log in, as your shell starts, before your setup files are read, your system probably has already set a default search path for you. Your system administrator can change that path. If your system has a default path, you should think about using it as part of your path - ask your administrator. To do that, include the variable $PATH or $path as you set your path. For example, to add your bin directory at the end of the system path, use one of the following lines: [4]

[4] There's a small problem with this if you set your path in your .cshrc or ksh ENV file. Each time you start a subshell (38.4 ) , your bin directory will be added to the path again. That won't cause any errors but it will make the path longer than it needs to be. If you want to work around this, use an environment variable like ENV_SET (2.7 ) as a flag - and set the path only if ENV_SET isn't set.

set path=($path ~/bin)                 C shell


PATH=$PATH:$HOME/bin                   Bourne shell

For Bourne-type shells, load the updated PATH by typing a command like:

$ . .profile

For the C shell, type one of these commands, depending on which file you changed:

% source .cshrc


% source .login

8.7.2 Changing Path on the Command Line

As you work, you might need to add a directory to your path temporarily. For example, when I develop new versions of existing programs, I put them in a separate directory named something like alpha-test . I don't usually want to run the alpha-test commands - but when I do, I add the alpha-test directory to the front of my path temporarily. (It's handy to set the new path in a subshell (38.4 ) so it won't change the path in my other shell.) Use the same path setting command you'd use in a shell setup file:

% set path=(~/xxx/alpha-test $path)

      C shell


$ PATH=$HOME/xxx/alpha-test:$PATH

        Bourne shell

$ export PATH

Article 8.8 shows another way to change your path: command-by-command instead of directory-by-directory.

- JP


Previous: 8.6 Output Command-Line Arguments UNIX Power Tools Next: 8.8 A Directory for Commands You Shouldn't Run
8.6 Output Command-Line Arguments Book Index 8.8 A Directory for Commands You Shouldn't Run

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System