Environment Variables

Environment Variables

In the configuration files for your shell, you will most commonly change the contents of various environment variables.

A variable is simply a piece of memory with a name. When the variable is used, the contents stored in memory are substituted for the name .

Usually, when a program stores something in a variable, the stored information is intended only for that program and is not available to any other program. (You can think of variables as private to the program that created them.) Environment variables are an exception to this.

Environment variables in Unix not only are available to the process that set them, but also are passed to any child processes at the moment the child process is created. (Review Chapter 2, "Using the Command Line," for an explanation of processes.) So if you set an environment variable in your shell, then any command you run from that shell will be able to read the contents of that variable. And any processes created by the child will also "inherit" all the environment variables that were set when it was created ( Figure 7.4 ). Note that shell variables, such as the prompt (which determines your shell prompt), are not passed to children.

Figure 7.4. Illustration of how environment variables set by parent processes are passed to child processes.


Many programs use this ability to read information provided by their parents and to configure their behaviorthink of it as programs' adapting to their environment.

When your shell starts up, it automatically sets a number of environment variables. You can create more and alter the ones that have already been set. By convention, the names of environment variables are capitalized, as in PATH . When you want to obtain the value that is stored in a variable, you add a $ to it. Adding the $ to the variable name tells the shell that the value stored in the PATH variable should be substituted at that point, as in $PATH . So the name of the variable is PATH , and to get its contents we use $PATH . If you have used the VisualBasic (or just BASIC) programming language, this will be familiar.

To see all your environment variables:

  • env

    The env command shows you all of the environment variables that are currently set ( Figure 7.5 ). For an explanation of common variables, see Table 7.3 .

    Figure 7.5. The command env shows all of the current environment variables.
     localhost:~ vanilla$  env  TERM=xterm-color SHELL=/bin/bash SSH_CLIENT=192.168.0.23 51989 22 SSH_TTY=/dev/ttyp1 USER=vanilla MAIL=/var/mail/vanilla PATH=/bin:/sbin:/usr/bin:/usr/sbin:/Users/vanilla/bin PWD=/Users/vanilla SHLVL=1 HOME=/Users/vanilla LOGNAME=vanilla SSH_CONNECTION=192.168.0.23 51989 175.122.221.13 22 _=/usr/bin/env OLDPWD=/Users/vanilla localhost:~ vanilla$ 

    Table 7.3. Common Environment Variables

    V ARIABLE

    M EANING

    HOME

    Full path of home directory

    SHELL

    Full path of login shell

    USER

    User name (a.k.a. short name in Mac OS X)

    PATH

    List of directories containing commands

    LOGNAME

    Same as USER

    PWD

    Full path of present working directory

    MANPATH

    List of directories containing man pages

    TERM

    Used to control how text is displayed in command-line applications

    VISUAL

    Name of the "visual" editor (such as vi , emacs , nano ) to be used when another program wants you to edit a file. The visual editor is expected to be one where you can see multiple lines of a file at the same time.

    EDITOR

    Name of an editor to be used when editing files. Some programs will use the value of this variable if the VISUAL variable is not set.


    The env command is actually a good example of a child's inheriting its environment from its parent. When you run the env command, your shell is the parent process that "spawns" a child process to run env . When env runs, it inherits its parent's environment and then reads and displays its own environment.

Tip

  • The tcsh shell has the setenv command, which is normally used to set an environment variable. If used with no arguments, setenv produces a neatly formatted list of all your environment variables.


You should leave most environment variables alonethey are set automatically to their correct values. For example, the USER variable has your user name, and changing it will simply confuse any program that tries to use it to determine which user you are.

Sometimes you will want to change environment variables, though; for example, the crontab program (used to schedule automatic execution of commands, discussed in Chapter 11, "Introduction to System Administration") reads the VISUAL environment variable to decide which editor to launch.

Setting environment variables is simple.

To change or create an environment variable in bash (or sh):

1.
VISUAL=vi

Make sure there are no spaces on either side of the equals sign.

2.
export VISUAL

The export command sends variables into the environment.

Without the export step, the VISUAL variable would be set, but only as a shell variable , which is a variable that the shell can read but that will not be passed on to any child processes it creates.

3.
Test the setting with echo $VISUAL ( Figure 7.6 ).

Figure 7.6. Setting an environment variable in the bash or sh shell.
 localhost:~ vanilla$  VISUAL=vi  localhost:~ vanilla$  export VISUAL  localhost:~ vanilla$  echo $VISUAL  vi localhost:~ vanilla$ 

Tips

  • The bash shell (but not the older sh shell) allows setting an environment variable in a single step using the export command:

    export VISUAL=vi

  • If you want the setting to be made each time you start a shell, put the two command lines' export command line in your ~/.profile file.

  • If the value contains spaces, make sure to enclose it in quotes:

     ORGANIZATION="Tony's Pizza" export ORGANIZATION 


To change or create an environment variable in tcsh (or csh):

1.
setenv VISUAL vi

This sets the environment variable named VISUAL to have the value vi .

2.
Test that it worked:

echo $VISUAL

Figure 7.7 shows the result.

Figure 7.7. Setting an environment variable in the tcsh or csh shell.
 [localhost:~] vanilla%  setenv VISUAL vi  [localhost:~] vanilla%  echo $VISUAL  vi [localhost:~] vanilla% 

Tips

  • As with the bash shell, if the value contains spaces, make sure to enclose it in quotes:

    setenv ORGANIZATION "Tony's Pizza"

  • If you want the setting to be made each time you start a tcsh or csh shell, put the two command lines' export command line in your ~/. cshrc file.

  • To reset (or unset , in Unix terminology) an environment variable, simply leave off the value:

    setenv VISUAL


Environment variables that are set at a shell prompt will last only for as long as you use the current shell in each session. That is, the setting disappears when you log out.

To make a durable change to an environment variable:

1.
Edit the shell configuration file in your home directory.

For bash , this would be

~/.bash_profile

For tcsh or csh , use

~/.cshrc

2.
Add the commands to the end of the file.

Use the commands described in the tasks abovefor example, to set the VISUAL variable to vi for the bash shell:

export VISUAL=vi

3.
Save the file.

4.
Quit the editor.

The change will take effect with the next shell you start.

5.
Open a new Terminal window.

6.
Test that the variable is set:

echo $VISUAL

Tips

  • A faster way to add a single line to a file is to use the output redirection you learned in Chapter 2. Replace steps 14 above with

     echo export ORGANIZATION=\"Tony\'s  Pizza\" >> ~/.bash_profile 

  • Make sure to use >> and not a single > character. If you use only > , you will wipe out the current contents of your .bash_profile file.

  • Notice how you have to escape the quotes by preceding them with backslashes so that the shell itself doesn't try to interpret them. You want the quotes to be part of the arguments to the echo command so that they end up in the ~/.bash_profile file.


Changing your PATH

Every time you execute a command by using only the command's name (for example, ls or pwd as opposed to the full path of the command, like /usr/bin/ls ), your shell looks for the command in a list of directories. That list is stored in the PATH environment variable. The PATH list provides a shortcut for finding commands.

If it weren't for the PATH list, you would have to type /bin/ls instead of ls , and /usr/bin/vi instead of vi . The command-line utilities supplied with the Mac OS X Developer Tools are located in /Developer/Tools (some of those utilities, such as GetFileInfo and SetFile , are described in Chapter 5, "Using Files and Directories"). The /Developer/Tools directory is not normally in your PATH , so if you use any of the commands in /Developer/Tools , you need to type their full pathnames, unless you add the directory /Developer/Tools to your PATH . Here's how to do that.

If you are using the bash shell, you can add a directory to your PATH by directly setting the PATH environment variable. Putting this setting in your ~/.bash_profile ensures that it takes effect each time you start a new shell.

Adding a directory to your PATH in bash:

1.
Edit your ~/.bash_profile file.

(Review Chapter 5 about editing files from the command line.)

If you are using the vi editor, the command is

vi ~/.bash_profile

2.
Add a line to the file that says

export PATH="$PATH:/Developer/Tools"

Notice how the new value includes the current value of $PATH , then a colon (no spaces!), and then the new directory. Without $PATH in the new value, the result would be to replace the old PATH with just the single new directorynot at all what you want.

3.
Save the file.

4.
Quit the editor.

The change takes effect the next time you start a shell.

5.
Open a new Terminal window to test the change.

6.
env

You'll see the new directory at the end of the list in the PATH variable.

Tip

  • To make the change for your current shell, execute the same command that you added to the .bash_profile file:

    export PATH="$PATH:/Developer/Tools"


If you are using the tcsh shell, you'll make the change in your ~/.tcshrc file.

Adding a directory to your PATH in tcsh:

1.
Start by editing your ~/.tcshrc file.

If you are using vi , the command is

vi ~/.tcshrc

2.
Add a line that says

set path = ( $path /Developer/Tools )

This may seem a little odd. After all, you are trying to set the PATH environment variable, not path . What's going on? Well, tcsh uses an unusual method to set the PATH variable.

tcsh requires that you set the shell variable path , and then tcsh sets the actual PATH environment variable. As discussed earlier, shell variables are created in your shell and are only available in the shell you are currently using. They are not passed on to child processes the way that environment variables are.

3.
Save your file (the command will depend on which editor you're using).

4.
Quit the editor (this command will also depend on which editor you're using).

The change will take effect in the next shell you start.

5.
Open a new Terminal window to test the change.

6.
env

You'll see the new directory at the end of the list in the PATH variable.



Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

Similar book on Amazon

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