Environment variables are dynamic values that the operating system and applications use to determine settings and configuration options.
These are used by applications launched in shells or sub-shells. These variables have a name and their respected value.
Setting Environment Variables in Linux
-
Temporary Environment Variables: Set for the duration of the terminal session.
VARIABLE_NAME=value
-
Example:
MY_VAR="Hello" echo $MY_VAR # Output: Hello
-
-
Permanent Environment Variables: Set permanently for all sessions.
-
Add the export command to profile files such as
.bashrc
,.bash_profile
, or/etc/profile
for system-wide settings. -
Syntax:
export VARIABLE_NAME=value
-
Example (add to
.bashrc
):export MY_VAR="Hello"
-
-
Viewing Environment Variables:
- Use the
printenv
orenv
command to list environment variables.
- Use the
-
Unsetting Environment Variables:
-
Remove an environment variable using the
unset
command. -
Example:
unset VARIABLE_NAME
-
-
Modifying Environment Variables:
-
Update the value of an existing environment variable.
-
Syntax:
export VARIABLE_NAME=new_value
-
Example:
export MY_VAR="NewValue"
-