Wednesday, January 8, 2014

Setting Environment Variables on Unix and Windows

In this post I will try and explain the different ways on how to set environment variables on both Windows and Unix operating systems. I am also going to address the scope when setting an environemt variable.

What environment variables are set on my machine?

You can easily check this by typing "set" into a command line terminal on both windows or unix.
Unix and windows:
set
How do you retrieve the value of a particular environment variable?
Unix:
echo $environment_variable
Windows:
echo %environment_variable%

What are environment variables?

Environment variables are operating system level variables that can affect the running of a process. This meaning is slightly obscure but let me try to explain it the way I understand it. The way I see it, environment variables are just aliases for file or directory paths. All they are are just short names or references to a directory on your system. The reason for their existence as you may have realised by now is that they are dynamic and are uniformly used in programs (ie. no hard coding paths).

There is however one special environment variable. This is the "path" variable. The path variable contains multiple directory locations separated by either a ";" on windows or ":" in unix. Each directory location references a program's binary files or executable files. As an example, if you were to type in "java" on the command line interface of both windows or unix, the OS would search each of the directories in the path variable and identify the executable or binary with the name java. When it identifies the executable matching the name, it will execute the process and terminate. This is a very interesting and important statement.

  1. The path variable is evaluated from left to right scanning each directory for an executable matching the name.
  2. It executes the first occurrence of the executable and terminates.
  3. If no matches are made, a command not found message is returned.

How to set environment variables? 


Environment variables can be set either programmatically or manually with both being equal but they can be set for different levels of scope.

Global Scope

This is the highest level and is available to all users of the system. 
Setting:
On unix this can be achieved by adding the environment variable to /etc/profile or /etc/profile.d file. On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a System Variable.
You will only be able to set a variable on this level if you are an administrator on the system.  

Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent.

User Scope

Environment variables set at this level is only available to the user that is logged in.
Setting:
On unix this can be achieved by adding the environment variable to /Users/<username>/.bash_profile file. Once an environment variable is set, you would need to log in again for the change to take effect or you can execute the .bash_profile script which will set the environment variable for the open session.

On windows you can set this by going to the properties of "My Computer" and then selecting the environment variables tab. Within this tab, you should see the System variables and User Variables. Set a User variable.
 
Once an environment variable is set, you would need to restart any open command terminals for the change to take effect. If you were to restart your machine the variable would still be there. It is permanent for the user.

Session Scope

Environment variables set at this level only last for the duration that the command line terminal is open. It is lost as soon as the terminal is closed.
Setting:
On unix this can be achieved by typing the following in a command line terminal: 
export VARIABLE=value  # for Bourne, Bash and related shells
setenv VARIABLE value  # for csh and related shells
On windows this can be achieved by typing the following in a command line terminal:
SET VARIABLE=value

Script Scope

Environment variables set at this level only last for the duration that the script runs. It is lost as soon as the script terminates, unless the script is executed from command line terminal. In this case the session scope rules apply.
Setting:
Same as in the session scope but is embedded in a shell or batch script.

Setting the "path" environment variable

All of the above applies to normal environment variables. Remember we said that the "path" variable is special and contains multiple directories. You can edit the path environment variable in the same way as mentioned for normal variables as above for the most part. 

Here are other variations which might be useful when setting the "path" environment variable.
When setting on windows use the ";" symbol as a separator:
SET path=C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the ":" symbol as a separator:
export path=/interesting/informing/techies:/important/informing/techies
If you wanted to append or prepend a new directory location to the existing "path" environment variable:      
When setting on windows use the %VAR% notation:
SET path=%path%:C:/interesting/informing/techies;C:/important/informing/techies
When setting on unix use the $VAR notation:
SET path=$path:/interesting/informing/techies;/important/informing/techies

Remember prepending or appending to the path variable makes a huge difference!!!

Who uses and maintains environment variables?

Applications and programs use environment variables. They also maintain the environment variables by doing the necessary creations, deletions and updates during installation, updating and uninstalling processes. 

Therefore, messing around with environment variables without a clear and proper understanding can cause your system not to work as desired. My suggestion is that if you do want to test something out, try it out on the session scope. In this way your system is not permanently affected. 

With this said, you can also use environment variables in your applications. They are great at abstracting paths and are also a solution to hard coding. 

Conclusion

I hope that this gives you a clear understanding of environment variables and their scope. As always, I would appreciate any feedback and comments are always welcome.

If you have issues in your environment I am more than happy to lend a hand!


2 comments:

  1. Nice post ! Not that environment variables can describe a lot of things other than files or paths; for example, in Linux you can have environment variables that describe the format of your command prompt, console output format, your default editor, and many more.

    ReplyDelete
    Replies
    1. Hi Gareth. Thanks for the comment. Yep, you are 100% correct. Environment Variables are not at all special.
      I was actually writing a post on installing Java. I was trying to explain how environment variables played it's role in the installation process. That's where I decided to first explain environment variables separately.

      So, watch this space for my next post ...

      Delete