Sunday, June 17, 2012

Environment variables in Ubuntu

Hi guys,

  • I have been using the .bashrc file for long time but it was today that I understood clearly the syntax of export in .bashrc file.
  • Before, going into that let's get to know about the environment variables little bit more.
  • When we open a terminal session and type in a command like ls it is searched in the directories that are marked by the PATH variable.
  • PATH is an environment variable.
  • An environment variable is a variable that persists for the life of a terminal session. The applications running in that session can access these variables. A listing of all the environment variables can be obtained by
    • abc@def:~$ export
  • PATH environment variable has a special format. Use echo command to have a look at the variable:
    • abc@def:~$ echo $PATH
    • /usr/local/bin:/bin:/usr/bin:/sbin:/usr/sbin:.
  • PATH variable is a : separated set of directories. All these directories will be searched when you execute a command in the terminal.
  • Let's give the PATH variable a different set of values
    • export PATH=/usr/bin:/usr/sbin
    • now, if we execute the ls command once again it will give an error because it no longer has /bin in the PATH variable.
  • Instead of changing or replacing the current set of directories in the PATH variable what you should do is to append it.
    • export PATH=$PATH:/new/path
    • echo $PATH  will now give you /usr/bin:/usr/sbin:/new/path
  • These changes, however, will be only valid to the current session. In order to making the changes permanent we can use a specific file called .bashrc file.
    • In Ubuntu (UNIX), files beginning with . are configuration files and are usually hidden.
    • To view these files use ls -a
  • The commands in .bashrc file get executed every time you start a new terminal
    • if you wish to have certain directories automatically added to PATH variable simply place those commands at the end of this file
 p.s.
  • If you are interested in knowing the full path of a binary like ls you can use the command which to find that out
    • e.g. abc@def:~$ which ls    will give you the result -> /bin/ls
      so, the executable ls is located in /bin directory


       

credit should go to the original contributor at http://www.cs.purdue.edu/homes/cs348/unix_path.html

No comments:

Post a Comment