Environment variables are set for use by programs.
System defined variables are set by the system. An example is pwd which holds the current working directory.
User defined variables are defined by the user either temporarily(for current shell only) or permanently.
Local environment variables are set for the current session only.
User environment variables are defined for the user and is used every time the user logs in. These variables are set in the files found in the user’s home directory:
- .bashrc – Loaded when new terminal is opened
- .bash_profile – Loaded when user logs in remotely. If not present .bash_login or .profile is used
System wide environment variables are available system wide (for all users). Defined in following directories and files:
- /etc/environment – System wide environment variable file
- /etc/profile – Like environment but for remote logins
List current environment variables
Remove local environment variable
Add local environment variable
The above will only be available during the session and will be cleared out when the session has ended.
To create a user environment variable edit the .bashrc file and add the export VAR_NAME=myvar/new command to the file. Save the file and use source .bashrc command to activate the changes.
To create a system environment variable edit the /etc/environment file and add the export VAR_NAME=myvar/new command to the file. Save the file and use source /etc/environment command to activate the changes.