Personal tools
You are here: Home Wiki Environment
Views
Environment copied.

Environment: What it is, how to set it.

The environment, in the context of a program, is a collection of data that persists across program invocations. The data can be thought as a set of special variables that don't get thrown away when a new process is created.

You can examine environment variables from your shell just as if they were a regular, normal sort of variable. The shell's expansion of $variable works for environment variables as well as local variables. You can also run the "env" command to display all of the environment variables.

However, to set an environment variable depends upon your shell. In bourne-type shells (sh/ksh/bash) you first set a local variable to the value you want, and then you promote that local variable to the "environment" by using export. For example:

 $ MYVAR"My Value"
 $ export MYVAR

In csh-type shells (csh, tcsh) you use the setenv command, like so:

 % setenv MYVAR "My Value"

Note that this differs from the way you set local variables in csh-type shells, which uses set and an equals.

If you really would rather set an environment variable on just one line, you don't have to use a csh-type shell. You can always use a semicolon as a command seperator, and pack two shell commands on one line:

 $ MYVAR"My value"; export MYVAR

And bash users have a further shortcut::

 bash$ export MYVAR"My Value"

But this can be regarded as a bashism, and you should know how to set an environment variable in the "usual" way in case you end up on a system that doesn't provide this sort of shortcut.

You tell me to add /sbin:/usr/sbin etc to my $PATH.........could you explain to me what this does and how do I add it?

Yes, if you're using a lot of the programs found in /sbin or /usr/sbin, then you can save yourself some typing by adding these directories into your PATH. You add these to your path by setting the __PATH__ environment variable.

The PATH environment variable is used by the shell to find the command that you have typed (if you don't specify a relative or absolute path). It looks in each directory in the PATH, in order, and when it finds a program that matches the command that you typed, it loads it and runs it in a sub-process.

You change the PATH like you would any other environment variable.

In sh-type shells:

 "$ PATH${PATH}:/sbin; export PATH"

In csh-type shells:

 "$ setenv PATH ${PATH}:/sbin"

In a csh-type shell, there is a magic local variable named "path" that you can use or change, and it will automatically change the PATH environment variable. For example, to add /sbin to your PATH, you'd type:

 % set path  ( $path /sbin )

Java environment

I'm playing with .jar files quite a bit but I need to preface each session with:

  PATH/usr/java/j2re1.4.1_01/bin:$PATH 

before running:

  java -jar jgraphpad.jar

How would I go about setting a global path for Java in my RH8 box?

If you want it for every user who uses the bash shell you could place your PATH... statement in /etc/bashrc

If you want it to apply only to your user you can place it at the bottom of your $HOME/.bashrc file.

I want to override an environmental variable for a single command only. Can you help?

Say for example you typically use gvim as your editor. The systems knows this because the environment variable EDITOR is set accordingly. However, suppose that one program does not get along well, say crontab, and you would prefer to use emacs instead. You can do::

$ EDITORemacs? crontab -e

This will run crontab, and crontab will launch emacs as its editor instead of gvim. Any future program that launches an editor will still launch gvim.

ThunderChicken sez: *I can't get this to work for me. In csh-land, you'd use parentheses to bracket it and have it run in a subshell, so:

 % (setenv EDITOR /usr/local/bin/emacs; crontab -e)



Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: