MorkaLork Development

Interesting stuff I've picked up over the years...

Environment variables

2010-01-31 12:41:33 | 291 views | linux environment environmental variable variables

Index






What are environment variables?


Environment variables are global variables that you can use in the shell. Before you set any by your self, there is a list of pre-set environment variables that both you and the system can use.
To see a list of the existing environment variables enter the command env:


maffelu@maffelu-laptop:~/shellScript$ env
ORBIT_SOCKETDIR=/tmp/orbit-maffelu
TERM=xterm
SHELL=/bin/bash
USER=maffelu
etc etc etc


Above is just a snippet, it's a fairly long list with already existing variables, but these can be used by you as well as the system. Let's try that out.

There is a variable called USER as we can see in the above output snippet. There is also a variable called HOME which represents the current user home directory. Let's echo the users name and home directory using these variables.
When using an environment variable it has to be prefixed with a dollar sign ($).


maffelu@maffelu-laptop:~/shellScript$ echo User $USER has home directory $HOME
User maffelu has home directory /home/maffelu


However, we can also create our own variables which can sometime come in handy. Say, for example, that we normally work on a project in a directory far, far away. We could set an environment variable to use to get to that directory.

Let's say we have a project in /home/maffelu/shellScript/fooProject/barProject. Now, to cd over there everytime you log into a shell can be quite annoying, so we create a variable containing that path for us:


maffelu@maffelu-laptop:~$ PROJECT=$HOME/shellScript/fooProject/barProject
maffelu@maffelu-laptop:~$ cd $PROJECT
maffelu@maffelu-laptop:~/shellScript/fooProject/barProject$


This is quite handy, so whenever we have a far away path, we can set it to an environment variable and then use it to navigate there.

There is one problem though. This will not be saved which means that whenever you terminate your terminal this variable will be unset and destroyed. So, we need to save it.

Back to top



Modify .bashrc


Now, .bashrc is a script that is run when you start up a new terminal. It contains some default settings and aliases that can come in handy.
We will add our PROJECT variable to the .bashrc file in order to to save it and always have it available.

Open the .bashrc file (/home/user/.bashrc) and go to the end of the file. The current contents of the file varies and we will not interfere with it.
At the end of the file add the following line:


export PROJECT=$HOME/shellScript/fooProject/barProject


The export statement sets the environment variable. Now, .bashrc is run at the start of a terminal session, so these changes won't take effect until we run the .bashrc script once more, so run this command in your home directory:


. .bashrc


Now the environment variables in .bashrc has been reset with our PROJECT variable, so if you run the command 'cd $PROJECT' now, no matter when or where, you will be transported to '/home/user/shellScript/fooProject/barProject/.

Back to top


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.