http://www.cs.hmc.edu/qref/changevar.html#bash on Environmental Variables
HMC Homepage CS Home

* What is an environment variable?
* Some examples of environment variables
* Why do I want to change it?
* How to change environment variables:
o in csh/tsch
o in bash
o in other shells



What is an environment variable?
Environment variables set defaults for your shell to use to make life easier. They store data like which directory to look in for your home directory, which text editor you prefer, or which directories to check when running programs. If you didn't have your path environment variable set, you would have to run /usr/local/bin/programname or /usr/sbin/programname or whichever directory the program is in followed by its name, rather than simply typing in the name of the program you want to run.

You can find out what your environment variables are in csh/tcsh by running setenv. In bash you should run export. To find out what a specific variable is set to, you can run echo $VARIABLENAME, which should work in any shell.

Some examples of environment variables

* PATH -- this sets the directories to look in for executable programs
* HOME -- this sets the location of your home directory
* EDITOR, VISUAL, TEXEDIT -- these set the program to use for viewing and editing files.
* LD_LIBRARY_PATH -- this sets the directories to look in to find library files required for programs to run.
* HOST, HOSTTYPE, VENDOR, OSTYPE, MACHTYPE, REMOTEHOST -- these contain the name of the server and information about its operating system.
* LINES, COLUMNS -- these set your terminal size
* INFOPATH, MANPATH -- these set the directories to look in for info files and man pages
* MAIL -- this sets the location of your mailbox
* JAVA_HOME, JAVA_FONTS, JAVA_COMPILER, CLASSPATH -- these set the environment for programming in java. CLASSPATH is the only one of these variables you should need to tamper with. CLASSPATH sets the location of java *.class files that are required to run any java program. If you are having problems with your classpath, try setting it to /usr/local/java-classes
* TERM -- this sets the type of terminal you run
* TZ -- this sets your local timezone
* SHELL -- this sets your default shell

There are other environment variables besides those listed. The quickest way to find out about them is either to ask someone who knows or search the web.

Why do I want to change it?
If you find yourself typing /usr/sbin/nslookup all the time or you know there's only one copy of a program installed but for some reason you have to specify the full path, you might want to change your PATH variable. If you try to run a program and it complains that it can't find the necessary library or java class, you might want to change LD_LIBRARY_PATH or CLASSPATH. When your account was created default values were provided for all these variables, but at some point you might need different ones. And besides, it's a lot easier than typing full paths all the time or editing the source of programs to find the right library.

How to change evironment variables:

in csh/tcsh:


If you want to temporarily set an environmental variables (that is, it will only apply to the current session, and when you log out and log in it will default back to whatever you previously had), you should run from the command line

setenv VARIABLENAME "value"

For example, if you want to set your path to first look in /bin, then /usr/bin, then /usr/sbin, then /usr/scb/bin, you would type:

setenv PATH "/bin:/usr/bin:/usr/sbin:/usr/ucb/bin"

If you want to set a variable for every time you login, you should add the setenv line to your .cshrc file in your home directory. If the variable is already set in that file, you may want to change the current line instead of adding another. Run source .cshrc to make the changes apply to the current session.

in bash:


To temporarily set environmental variables for the current session, you should run

export VARIABLENAME="value"

For example, to set your path as mentioned above, you would type:

export PATH="/bin:/usr/bin:/usr/sbin:/usr/ucb/bin"

As in csh/tcsh, to set the variable for all sessions, you should add it to the .bashrc file in your home directory, then run source .bashrc to apply the changes.

in other shells:


Most shells accept either setenv or export. For specifics on your shell, check your shell's man page.


Comments

Popular posts from this blog

Impossible