What are Environment variables?
Environment variables are dynamic values which affect the processes
or programs on a computer. They exist in every operating system, but
types may vary. Environment variables can be created, edited, saved,
and deleted and give information about the system behavior.
Environment variables can change the way a software/programs
behave.
E.g. $LANG environment variable stores the value of the language that the user
understands. This value is read by an application
Let's study some common environment variables -
Variable Description
This variable contains a colon (:)-separated list of
directories in which your system looks for executable files.
PATH When you enter a command on terminal, the shell looks
for the command in different directories mentioned in the
$PATH variable. If the command is found, it executes.
Otherwise, it returns with an error 'command not found'.
USER The username
HOME Default path to the user's home directory
EDITOR Path to the program which edits the content of files
UID User's unique ID
TERM Default terminal emulator
SHELL Shell being used by the user
Accessing Variable values
In order to determine value of a variable, use the command
echo $VARIABLE
The 'env' command displays all the environment variables.
Set New Environment Variables
You can create your own user defined variable, with syntax
VARIABLE_NAME= variable_value
Deleting Variables
The following syntax can be used to remove a Variable from the
system.
unset variablename
Summary:
Environment variables govern the behavior of programs in your Operating System.
Command Description
echo $VARIABLE To display value of a variable
env Displays all environment variables
VARIABLE_NAME= variable_value Create a new variable
unset Remove a variable
export Variable=value To set value of an environment variable
putenv - change or add an environment variable
int putenv(char *string);
The putenv() function adds or changes the value of environment
variables. The argument string is of the form
name=value. If name
does not already exist in the environment, then string
is added to
the environment. If name does exist, then the value of
name in the
environment is changed to value.
getenv - get an environment variable
char *getenv(const char *name);
The getenv() function searches the environment list to find
the
environment variable name, and returns a pointer to the
corresponding value string.