## Recognize, view and modify environment variables ##

[Author](authors.html): _Ivan Voras_ _IvanVoras_ FreeBSD

[Reviewer](technical_reviewers.html): _name_ _[[contact]]_ _[[BSD_flavour]]_

[Reviewer](technical_reviewers.html): _name_ _[[contact]]_ _[[BSD_flavour]]_

----



### Concept ###

Be able to view and modify environment
variables(((variables)))(((environment variables))) both temporarily
and permanently for each of the default shells(((shell))) found
on BSD systems.


### Introduction ###

Environment variables are key-value pairs available to executing processes. By way of environment variables, users (and other processes) can pass data to new processes. Both keys and values can only be strings, and both are usually case sensitive. In shell scripts interpreted by `/bin/sh` (as well as many others), environment variable contents are referenced by `${KEY}`. 

Some environment variables need only to be set, without regards for their content (one example is the `DEBUG` variable), and there are usually command shortcuts for this operation.

Most shells have their own internal variables, which should not be confused with global environment variables as they are not passed to newly started processes.

Different shells have different commands for manipulating environment variables. Read more about their syntax in the appropriate manual pages.

#### sh(((sh))), ksh(((ksh))), bash(((bash))) ####

Internal shell variables can be set simply by issuing a statement like "`key=value`", and inspected with the `set` command.. An internal variable can then be promoted to a global environment variable with the `export` command. Internal shell variables can be deleted with `unset`. If the internal variable was exported at the time, it will also be deleted from the environment.

Shell variables are only valid within a shell process instance (spawned subshells(((subshells))) will not contain their parent's internal variables).

In order to just set an environment variable with empty content, use the form "`export NAME`" without defining the internal shell variable.

#### csh(((csh))), tcsh(((tcsh))) ####

Internal shell variables can be set and inspected with the `set` command, and environment variables by the `setenv` command. Internal shell variables can be deleted with `unset`, and environment variables deleted with `unsetenv` command. 

To set an environment variable to empty content, use `setenv NAME`.

#### Common environment variables ####

There are environment variables which have well defined meanings for a Unix process. Some of them are:

 * `USER` : Currently logged-in user (e.g. `username`)
 * `HOME` : Currently logged-in user's home directory (e.g. `/home/ivoras`)
 * `TERM` : Active terminal (console) type (e.g. `xterm`)
 * `EDITOR` : User's preferred text file editor (e.g. `vi`)
 * `VISUAL` : User's preferred visual file editor (e.g. `emacs`)
 * `PAGER` : User's preferred pager (e.g. `/usr/bin/more`)
 * `PATH` : User's search path for executables (e.g. `/bin:/usr/bin:/usr/local/bin`)

### Examples ###

Automatically set and export an environment variable called "VEGETABLE"  to "Carrot", in _bash_:

    $ export VEGETABLE=Carrot

Create an environment variable called "VEHICLE" containing the string "Truck", in _tcsh_:

    > setenv VEHICLE Truck

List environment variables, in _tcsh_:

    > setenv

Note that _(ba)sh_ uses "=" to set enviroment variables, and _(t)csh_ doesn't.

### Practice Exercises ###

 1. Investigate what does `PWD` environment variable do
 2. Experiment with setting the `PAGER` environment variable and the behavior of the manual page viewer (`man`)
 3. Investigate how does internal variable `shlvl` behave in _(t)csh_ when spawning subshells
 4. Unset the `PATH` environment variable and see if you can start programs without specifying their full path

### More information ###

env(1), sh(1), csh(1), tcsh(1), environ(7)


