fork
and
exec
. Alternatively, from
command shells
such as
bash
, a user can change environment variables for a particular command invocation by indirectly invoking it via
env
or using the
ENVIRONMENT_VARIABLE=VALUE <command>
notation. All
Unix
operating system
flavors,
DOS
, and
Windows
have environment variables; however, they do not all use the same variable names. A running program can access the values of environment variables for configuration purposes.
- PATH - a list of directory paths. When the user types a command without providing the full path, this list is checked to see whether it contains a path that leads to the command.
- HOME (Unix-like) and USERPROFILE (Microsoft Windows) - indicate where a user's home directory is located in the file system.
- HOME/{.AppName} (Unix-like) and APPDATA\{DeveloperName\AppName} (Microsoft Windows) - for storing application settings. Many applications incorrectly use USERPROFILE for application settings in Windows - USERPROFILE should only be used in dialogs that allow user to choose between paths like Documents/Pictures/Downloads/Music, for programmatic purposes APPDATA (roaming), LOCALAPPDATA or PROGRAMDATA (shared between users) is used.
- TERM (Unix-like) - specifies the type of computer terminal or terminal emulator being used (e.g., vt100 or dumb).
- PS1 (Unix-like) - specifies how the prompt is displayed in the Bourne shelland variants.
- MAIL (Unix-like) - used to indicate where a user's mail is to be found.
- TEMP - location where processes can store temporary files
COMMAND.COM
's existence.
AUTOEXEC.BAT
file.
$LD_LIBRARY_PATH
and
$LD_PRELOAD
and run it with the process's authority. If a setuid program did this, it would be insecure, because its caller could get it to run arbitrary code and hence misuse its authority. For this reason, libc unsets these environment variables at startup in a setuid process. setuid programs usually unset unknown environment variables and check others or set them to reasonable values.
echo $HOME
In DOS, OS/2 and Windows command-line interpreters such as COMMAND.COM
andcmd.exe
, the user has to type this:
ECHO %HOME%
In Windows PowerShell, the user has to type this:
Write-Output $env:HOMEPATH
env
, set
, and printenv
display all environment variables and their values.
printenv
can also be used to print a single variable by giving that variable name as the sole argument to the command.
Unix[edit]
In Unix, the following commands can also be used, but are often dependent on a certain shell.
VARIABLE=value # export VARIABLE # for Bourne and related shells
export VARIABLE=value # for ksh, bash, and related shells
setenv VARIABLE value # for csh and related shells
export
keyword. Variables defined in this way are displayed by theset
command, but are not true environment variables, as they are stored only by the shell and not recognized by the kernel. The
printenv
command will not display them, and child processes do not inherit them.
VARIABLE=value
However, if used in front of a program to run, the variables will be exported to the environment and thus appear as real environment variables to the program:
VARIABLE=value program_name [arguments]
.profile
or .bashrc
or your current shell profile file (located in your home directory). Then, each time you open your shell it will be loaded.
To change the environmental variable "permanently" you'll need to consider at least these situations:
- Login/Non-login shell
- Interactive/Non-interactive shell
bash
- Bash as login shell will load
/etc/profile
,~/.bash_profile
,~/.bash_login
,~/.profile
in the order - Bash as non-login interactive shell will load
~/.bashrc
- Bash as non-login non-interactive shell will load the configuration specified in environment variable
$BASH_ENV
$EDITOR ~/.bashrc
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
zsh
$EDITOR ~/.zshrc
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
ksh
$EDITOR ~/.profile
#add lines at the bottom of the file:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
export ORACLE_HOME=/usr/lib/oracle/11.2/client64
bourne
$EDITOR ~/.profile
#add lines at the bottom of the file:
LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
ORACLE_HOME=/usr/lib/oracle/11.2/client64
export LD_LIBRARY_PATH ORACLE_HOME
csh or tcsh
$EDITOR ~/.login
#add lines at the bottom of the file:
setenv LD_LIBRARY_PATH /usr/lib/oracle/11.2/client64/lib
setenv ORACLE_HOME /usr/lib/oracle/11.2/client64
/etc/profile
or
/etc/environment
.
DOS, OS/2 and Windows[edit]
In DOS, OS/2 and Windows command-line interpreters such as COMMAND.COM
andcmd.exe
, the SET
command is used to assign environment variables and values using the following arguments:
SET VARIABLE=value
SET
command without any arguments displays all environment variables along with their values.