How do I set environment variables on UNIX systems?
UNIX and all UNIX-like operating systems such as OpenBSD, Linux, Redhat, CentOS, Debian allows you to set environment variables. When you log in on UNIX, your current shell (login shell) sets a unique working environment for you which is maintained until you log out. Following are most command examples of environment variables used under UNIX operating systems:
- PATH – Display lists directories the shell searches, for the commands.
- HOME – User’s home directory to store files.
- TERM – Set terminal emulator being used by UNIX.
- PS1 – Display shell prompt in the Bourne shell and variants.
- MAIL – Path to user’s mailbox.
- TEMP – Path to where processes can store temporary files.
- JAVA_HOME – Sun (now Oracle) JDK path.
- ORACLE_HOME – Oracle database installation path.
- TZ – Timezone settings
- PWD – Path to the current directory.
- HISTFILE – The name of the file in which command history is saved
- HISTFILESIZE -The maximum number of lines contained in the history file
- HOSTNAME -The system’s host name
- LD_LIBRARY_PATH -It is a colon-separated set of directories where libraries should be searched for.
- USER -Current logged in user’s name.
- DISPLAY -Network name of the X11 display to connect to, if available.
- SHELL -The current shell.
- TERMCAP – Database entry of the terminal escape codes to perform various terminal functions.
- OSTYPE – Type of operating system.
- MACHTYPE – The CPU architecture that the system is running on.
- EDITOR – The user’s preferred text editor.
- PAGER – The user’s preferred text pager.
- MANPATH – Colon separated list of directories to search for manual pages.
Display Environment Variable
Open the terminal and type the following commands to display all environment variables and their values under UNIX-like operating systems:
$ set
OR
$ printenv
OR
$ env
Sample outputs:
To display search path, enter:
echo $PATH |
To display prompt settings, enter:
echo $PS1 |
A few more examples:
echo $USER echo $PWD echo $MAIL echo $JAVA_PATH echo $DB2INSTANCE |
Change or Set Environment Variable
You can use the following command to change the environment variable for the current session as per your shell.
For Korn shell (KSH)
The syntax is as follows:
var=value export var |
To set JAVA_PATH, enter:
JAVA_PATH=/opt/jdk/bin export JAVA_PATH |
For Bourne shell (sh and bash)
The syntax is as follows:
export var=value |
To set PATH, enter:
export PATH=$PATH:/opt/bin:/usr/local/bin:$HOME/bin |
For C shell (csh or tcsh)
The syntax is as follows:
setenv var value |
Set EDITOR to vim, enter:
setenv EDITOR vim |
Example: UNIX C Shell Startup Configuration Files For Environment Variable
C shell use the following files:
- /etc/csh.login – It is executed if C shell is your login shell.
- $HOME/.cshrc and $HOME/.login – These files are executed every time C Shell starts. The ~/.login is csh login script, read by login shell, after ~/.cshrc at login.
The above set or setenv commands can be placed in the ~/.cshrc or ~/.login files. A sample $HOME/.cshrc file is as follows:
alias h history 25 alias j jobs -l alias la ls -a alias lf ls -FA alias ll ls -lA umask 22 set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin) setenv EDITOR vi setenv PAGER more setenv BLOCKSIZE K if ($?prompt) then # An interactive shell -- set some stuff up set filec set history = 100 set savehist = 100 set mail = (/var/mail/$USER) if ( $?tcsh ) then bindkey "^W" backward-delete-word bindkey -k up history-search-backward bindkey -k down history-search-forward endif endif # Traps CTRL-D's to avoid accidental system log off set ignoreeof # Set prompt set prompt = "[\!] %" # Sequentially keeps a buffer of your last events. set history=100 set savehist=100 # Stops C Shell from overwriting and destroying the information in an existing file. set noclobber |
A sample ~/.login file is as follows:
# Show fortune :) if ( -x /usr/games/fortune ) /usr/games/fortune # Sets the system variable TERM to recognize the xterm setenv TERM xterm # This command sets the time zone variable setenv TZ IST # set PATH setenv PATH /opt/gnu/bin:/bin/posix:/bin:/usr/bin:/usr/local/bin:/etc:/users/vivek:. # set mail box set mail=/usr/mail/vivek # alias bye is easier to remember alias bye logout alias c clear # read mail as soon as I get into the systems mutt |
Example: UNIX KSH Shell Startup Configuration Files For Environment Variable
KSH shell use the following files:
- /etc/profile – This default system file is executed by the KSH and sets up default environment variables.
- $HOME/.profile – Put your customization in this file.
A sample $HOME/.profile for the ksh shell:
PATH=/opt/gnu/bin:/bin/posix:/usr/bin:/usr/lib:/bin:/users/v/vivek/bin MAIL=/usr/mail/vivek HOME=/users/vivek EDITOR=/opt/gnu/bin/vim START=~/.kshrc TERM=xterm # export it export ENV START EDITOR TERM PATH MAIL HOME stty sane susp ^Z # email notification if mail -e then echo "You have mail." fi # prompt PS1="$ " # Check system messages msgs -q # Allow terminal messages mesg y |
Recommend reading:
See ksh and csh shell man page for more details.
Lab 2
Complete the following steps. Write your answer in the space provided.
1. Which specific shell characters have special meaning to the shell?
_____________________________________________________________
2. Name some common shell metacharacters.
_____________________________________________________________
3. Which metacharacter is a shell substitute for the home directory of a
user?
_____________________________________________________________
4. Navigate to your home directory from your current working
directory using the appropriate special metacharacter.
5. Which two metacharacters are often referred to as wildcard
characters?
6. Make sure you are in your home directory. List the contents of all the
files and directories in your home directory starting with d using a
wild card entry.
7. Which character do you use to match a single character, excluding a
leading period?
_____________________________________________________________
8. Which character or characters would you use to match a set or range
of characters?
_____________________________________________________________
9. Which character or characters would you use to have the shell ignore
the special meaning of metacharacters?
_____________________________________________________________
10. What are file descriptors?
_____________________________________________________________
_____________________________________________________________
11. Which symbol or symbols do you use to redirect output and append
the output to a file?
_____________________________________________________________
12. Which command redirects standard error messages?
_____________________________________________________________
13. Which symbol or character do you use to connect two or more
commands on a single command line?
_____________________________________________________________
14. Define a variable. Name the kinds of variables used in Korn shell
programming.
_____________________________________________________________
_____________________________________________________________
15. Which command do you use to display shell variables and their
current values?
_____________________________________________________________
16. Which command do you use to display a list of previously executed
commands in the shell?
____________________________________________________________________________________________________________________________________________________________17. What is an initialization file?
_____________________________________________________________
_____________________________________________________________
18. List the four user initialization files described in this module.
_____________________________________________________________
_____________________________________________________________
_____________________________________________________________
19. Edit your ~/.profile file to set the ENV variable to $HOME/.kshrc.
Also add /etc to your path.
20. Edit your ~/.kshrc file to set the prompt to be hostname and the
current directory.
21. Log out and log in again to check that your settings work.
thank you so much
how can i set or change the environment variables permanently.
Plz help.
sir we have test environment for our testing but someone accidently delete the directory we want to restore it again . but as i know it is impossible if some thing delete from unix server it is impossible to recover. But we try to rebuild our test environment take data from live server but it is not configured with us bcoz we dont know the port no , configure ip etc so please guide us.