Pages

Set Unix Environment Variables

It could be tricky to set environment variables on Unix correctly because the syntax of the commands varies depending on what Unix shell you have to use. Therefore, the first thing necessary is to find out what type of Unix Shell you have landed on: - there are many possibilities: Bourne (sh), Korn (ksh),  C shell (csh), and others.  Despite of the varieties, the command syntax generally falls into the first three as mentioned above.For instance, the Bash Shell utilizes Bourne syntax, while Tcsh utilizes C Shell syntax.

Be careful that, while the Bourne shell (sh) and Korn shell (ksh) typically use either a '#' or a '$' as a command line prompt, and the C Shell (csh) typically uses a '%' as a command line prompt, a '>' is used in the following examples to represent the command line prompt.

Determine the Type of Unix Shell

> env | grep SHELL

        -or-

        > echo $SHELL

  -or-

> ps -f        ....Will provide a full listing of processes associated
                       with the current terminal, one of which will be the
                       shell process.

-or-

> setenv       ....On a C shell this will return the current
                       environment, while other shells will return an error.

The following examples use 'ORACLE_HOME' as the variable name.

BOURNE SHELL(sh)

To set environment variables within a Bourne Shell (sh), the variable must be
initialized locally, then exported globally:

> ORACLE_HOME=/u01/app/oracle/product/8.1.7
                ...defines ORACLE_HOME locally to the shell

> export ORACLE_HOME
                ...makes it globally available to other processes started from
                this shell

To have a variable set automatically when you log into the Bourne Shell of your
Unix server:

Add the above lines (minus the '>' prompt) to the hidden '.profile'
        file in your $HOME directory.

If you make changes to your '.profile' and want those changes propagated to
your current running environment (without having to log out, then back in):

        > cd $HOME
> . .profile

To unset environment variables within a Bourne Shell (sh):

> unset ORACLE_HOME

To check what an environment variable is set to:

> env | grep ORACLE_HOME

KORN SHELL(ksh)

To set environment variables within a Korn Shell (ksh), you can use the Bourne
syntax show above, or use the streamlined Korn Shell syntax:

> export ORACLE_HOME=/u01/app/oracle/product/8.1.7

To have a variable set automatically when you log into the Korn Shell of your
Unix server:

Add the above lines (minus the '>' prompt) to the hidden '.profile'
        file in your $HOME directory.

If you make changes to your '.profile' and want those changes propagated to
your current running environment (without having to log out, then back in):

> cd $HOME
> . .profile

To unset environment variables within a Korn Shell (ksh), use the same syntax
as you would in a Bourne Shell (sh):

> unset ORACLE_HOME

To check what an environment variable is set to:

> env | grep ORACLE_HOME


C SHELL(csh)

To set environment variables within a C Shell (csh):

> setenv ORACLE_HOME /u01/app/oracle/product/8.1.7

To have a variable set automatically when you log into the C Shell of your Unix
server:

Add the above lines (minus the '>' prompt) to the hidden '.login' file
        in your $HOME directory.

If you make changes to your '.login' and want those changes propagated to your
current running environment (without having to log out, then back in):

> cd $HOME
> source .login

To unset environment variables within a C Shell (csh):

> unsetenv ORACLE_HOME

To check what an environment variable is set to:

> env | grep ORACLE_HOME


Handle Errors

If You Encounter Errors Using the Above Commands:

Check the man page for the Unix shell you are using:

> man sh

Since Unix shell implementations vary from platform to platform, and Unix
shells are highly configurable, it's possible that the information supplied
above is not correct for the Unix platform you are on.  Please check with your
system administrator if you have any further questions or problems setting
environment variables.

1 comment:

  1. Setting up a Unix environment is essential for developers and system administrators. For students in Pakistan requiring academic writing assistance,academic writing services in pakistan can provide the necessary support to excel in their studies while mastering Unix environments.

    ReplyDelete