OpenWindows Advanced User's Guide

10.2 Environment Variables

Your system sets up your system environment using a set of specifications defined in the initialization files. If you want to temporarily modify your environment for the current work session you can issue commands directly at the command prompt. However, if you want to modify your working environment on a more permanent basis, you can store "permanent" environment variables in the .profile, .login, or .cshrc files.

To display the environment variables currently set on your system:

    Type the env command and press Return:

    $ env
    HISTORY=100
    HOME=/home/texas/keith
    HZ=100
    LOGNAME=keith
    MAIL=/var/mail/keith
    MANSECTS=\1:1m:1c:1f:1s:1b:2:\3:3c:3i:3n:3m:3k:3g:3e:3x11:3xt:3
    w:3b:9:4:5:7:8
    PATH=/usr/bin
    SHELL=/bin/sh
    TERM=sun
    TZ=EST5EDT


    Note -

    You can also use the env command to identify your login shell. It is specified in the SHELL environment variable. In the example above, the shell is set to /bin/sh (the Bourne shell).


10.2.1 The User Profile

This section describes some of the more commonly used environment variables. Many of these variables may already be in your user profile. As previously mentioned, your user profile file (.profile for the Bourne and Korn shells and .cshrc for the C shell) is located in your home directory.


Note -

Hidden ("dot") files can be listed by typing ls -la.


The following is a partial list of environment variables that can be included in your user profile. The syntax for defining environment variables depends on the shell you're using:

10.2.2 Setting the PATH Variable

The PATH environment variable is used to locate commands within the SunOS directory hierarchy. By setting the PATH you create a fixed set of directories that the system always searches whenever you enter the name of a command.

For example, if you have no PATH variable set and you want to copy a file, you need to enter the full pathname for the command, /usr/bin/cp. However, if you have set the PATH variable to include the directory /usr/bin, then you can simply type cp and your system will always execute the command. This is because your system searches for the cp command in every directory named in the PATH variable, and executes it when it is found. Using the PATH variable to list the commonly used SunOS command directories can thus significantly streamline your work.

For the Bourne and Korn shells, the PATH variable is specified in your .profile file (in your home directory) using the following syntax:

PATH=.:/usr/bin:/home/bin

where home represents the path name of your home directory.

For the C shell, the PATH variable is specified in your .cshrc file (in your home directory) using the following syntax:

set path=(. /usr/bin home/bin)

where home is the path name of your home directory.


Note -

In the C shell you can use the shortcut ~ to represent the path name of your home directory.


If you modify the PATH variable, and you are running the C shell, use the source command to make the changes effective in your current window without having to logout:

example% source .cshrc 

If you are running the Bourne or Korn shell, type the following to make the changes effective in your current window without having to logout:

$ . .profile 

10.2.3 Aliases (C Shell Only)

Aliases are useful shortcuts for commands you often type. For example, the default setting for the remove command (rm) does not ask for confirmation before removing files. Sometimes this is inconvenient, as a typing error can remove the wrong file. However, the C shell lets you use the alias variable to change this by adding the following line to your .cshrc file:

alias rm  'rm -i'

With this line in the .cshrc, typing rm will now be the same as typing rm -i, which is the interactive form of the rm command. You will then always be asked to confirm the command before any files are deleted. (The quote marks around rm -i in the example above are necessary to include the blank space between rm and -i. Without them the C shell cannot correctly interpret the text after the space.

To make your changes to the .cshrc file effective immediately in your current window, use the source command. The source command causes the system to read the current .cshrc file and execute the commands in it:

example% source .cshrc

10.2.4 Changing Your Command Prompt

The syntax you use to change your command prompt depends on whether you are using the Bourne, Korn or C shell.

10.2.4.1 Bourne and Korn Shells

For the Bourne or Korn shells, you redefine your command prompt with the PS1 command. The following are three examples:

PS1=": "
PS1="`hostname`: "
PS1="`hostname`{`id`}}: "

Type any of the examples above to change your current command prompt. The prompt will remain until you change it again, or logout.

If you want to make your changes more permanent, add one of the above examples (or a prompt of your own creation) to your .profile file. If you do this, the prompt you specify will appear each time you login in or start a new shell.

10.2.4.2 C Shell

For the C shell, you personalize your command prompt with the set prompt command. The following are three examples:

set prompt="% "
set prompt="`hostname`\!: "
set prompt="`hostname`{`id`}}: "

Type any of the examples above to change your current command prompt. The prompt will remain until you change it again, or logout.

If you want to make your changes more permanent, add one of the above examples (or a prompt of your own creation) to your .cshrc file. If you do this, the prompt you specify will appear each time you login in or start a new shell.

10.2.5 Other Useful Variables

There are many other variables which you can set in your .profile or.cshrc files. For a complete list, refer to the man Pages(1): User Commands. The following are a few brief descriptions of some of the more commonly used options.

Use set noclobber to prevent unintentional overwriting of files when you use the cp command to copy a file. This variable affects the C shell only. Enter the following in your .cshrc file:

set noclobber

Use set history to set the number of commands saved in your history list. The history command is useful to view commands you have previously entered. The history file can also be used to repeat earlier commands. This variable affects the C shell only. Enter the following in your .cshrc file:

set history=100

You can also affect the Bourne and Korn shells in the same manner by placing the following in your .profile file:

HISTORY=100