Setting Environment Variables How To

From SwinBrain

In this page we will look at setting the value of an environment variable. This how to covers setting environment variables in Windows, Mac OS, and Linux.

This is a How To article designed to give step-by-step instructions. Search SwinBrain and the external links if you require more detailed information about this topic.

Contents

Changing the Current Session

Each session has its own environment variables. You can add and change environment variables within the session which will then disappear when the session is closed. This is good when you need to change the environment temporarily.

Try the following in Windows.

  1. Open a command prompt
  2. Type the following commands to view and modify the path environment variable
echo %PATH%
set PATH=c:\apps\putty;%PATH%
echo %PATH%

Try the following in Mac OS.

  1. Open a terminal window
  2. Type the following commands to view and modify the path environment variable
echo $PATH
PATH=~/bin:$PATH
echo $PATH

Try the following in Linux.

  1. Open a terminal window
  2. Type the following commands to view and modify the path environment variable
echo $PATH
PATH=~/bin:$PATH
echo $PATH

Making a Lasting Change

Temporary changes are useful sometime, but many times you will want to alter environment variables permanently, ensuring that they are set by default.

Setting Environment Variables in Windows

Setting the Environment variable

Changes to the default environment variables are made via the System Properties dialog.

  1. Locate your MyComputer icon, right click it and select Properties.
  2. Switch to the Advanced tab and click Environment Variables
  3. To add an environment variable for only your user use the User Variables list, otherwise use the System Variables list.
  4. Use the New button to add a new environment variable to the list, or the Edit button to modify an existing variable
  5. Click ok to close all dialogs

To verify that this change has worked open a command prompt and use echo %name% to show the value of the environment variable called name.

The content of this section was created for Windows XP SP2. This may not be relevant for different versions of this software.

Setting Environment Variables in Mac OS

Setting environment variables in .profile

To permanently change an environment variable in Mac OS you need to export the variable from your .profile script.

  1. Open a text editor such as Smultron
  2. Open ~/.profile
  3. Add export NAME=value to export the environment variable $NAME


The content of this section was created for Bash & Mac OS 10. This may not be relevant for different versions of this software.

Setting Environment Variables in Linux

To permanently change an environment variable in Linux you need to export the variable from your .login script.

  1. Open a text editor
  2. Open ~/.login
  3. Add export NAME=value to export the environment variable $NAME
The content of this section was created for Bash & CentOS. This may not be relevant for different versions of this software.

[edit]Links: Topic Brains
Return to Swin Brain