≡ Menu

UNIX: Set Environment Variable

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
$ printenv
$ env
Sample outputs:

Fig.01: Displaying all environment variables and their values command

Fig.01: Displaying all environment variables and their values command

To display search path, enter:

echo $PATH

To display prompt settings, enter:

echo $PS1

A few more examples:

echo $USER
echo $PWD
echo $MAIL

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:

export var

To set JAVA_PATH, enter:

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:

  1. /etc/csh.login - It is executed if C shell is your login shell.
  2. $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
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
# 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

Example: UNIX KSH Shell Startup Configuration Files For Environment Variable

KSH shell use the following files:

  1. /etc/profile - This default system file is executed by the KSH and sets up default environment variables.
  2. $HOME/.profile - Put your customization in this file.

A sample $HOME/.profile for the ksh shell:

# export it
stty sane susp ^Z
# email notification 
if mail -e
   echo "You have mail."
# prompt
PS1="$ "
# Check system messages
msgs -q
# Allow terminal messages
mesg y

Recommend reading:

See ksh and csh shell man page for more details.

{ 3 comments… add one }

  • Baljaa September 19, 2013, 10:33 am

    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
    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
    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
    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.

  • sivakrishna September 27, 2013, 8:27 am

    thank you so much

  • pankaj November 4, 2014, 3:21 pm

    how can i set or change the environment variables permanently.

    Plz help.

Leave a Comment

   Tagged with: , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,