mydomain
No ADS
No ADS

Setup environment variables on Ubuntu

  1. List environment variables
  2. Check for a specific environment variable
  3. Setup temporary environment variables
  4. Set system-wide permanent environment variables
  5. Set permanent environment variables for specific user
Environment variables are global variables used by users and processes in the operating system. In Ubuntu, there are two types of environment variables: temporary environment variables and permanent environment variables.
Permanent environment variables are also divided into two types:
  • System-wide environment variables
  • Permanent environment variables for specific user.
In this article, I will guide you to view, add, update and delete environment variables in Ubuntu.

1. List environment variables

To list current environment variables use the command below in Terminal:
printenv
The results you get:
karakol@karakol-VirtualBox:~$ printenv
SHELL=/bin/bash
SESSION_MANAGER=local/karakol-VirtualBox:@/tmp/.ICE-unix/1597,unix/karakol-VirtualBox:/tmp/.ICE-unix/1597
QT_ACCESSIBILITY=1
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
SSH_AGENT_LAUNCHER=gnome-keyring
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_SHELL_SESSION_MODE=ubuntu
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
_=/usr/bin/printenv

2. Check for a specific environment variable

If you want to display the value of a specific environment variable just use the echo command:
echo $[variable_name]
Example:
karakol@karakol-VirtualBox:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

3. Setup temporary environment variables

Temporary environment variables are set during a session and persist for that session; they are removed after the session ends.
When you open Terminal and set an environment variable, it exists only in this Terminal window, and is not shared with other Terminal windows. Temporary environment variables are useful so you don't have to type the same value multiple times when working with Terminal.
export [variable_name]=[variable_value]
For example, set an environment variable, and view its value:
karakol@karakol-VirtualBox:~$ export greeting="Hello Everybody"
karakol@karakol-VirtualBox:~$ echo $greeting
Hello Everybody
karakol@karakol-VirtualBox:~$ export greeting2=Hi
karakol@karakol-VirtualBox:~$ echo $greeting2
Hi
For example, add a directory path to the system's current PATH environment variable.
karakol@karakol-VirtualBox:~$ export PATH=/Users/test/test_folder:$PATH
karakol@karakol-VirtualBox:~$ echo $PATH
/Users/test/test_folder:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

4. Set system-wide permanent environment variables

There are two types of permanent environment variables in Ubuntu:
  • System-wide permanent environment variables (for all users).
  • Permanent environment variable for a specific user.
System-wide permanent environment variables are stored in the /etc/profile file.
You can use vim or nano commands to modify the contents of this file:
sudo nano /etc/profile
  • Ctrl + O --> Save
  • Ctrl + X --> Exit
Note: In some versions of Ubuntu, environment variables in files are not read each time you start a new session. In that case, every time you start a new session you have to read this file manually:
source /etc/profile

5. Set permanent environment variables for specific user

Permanent environment variables for a specific user are stored in the files "~/.bashrc" and "~/.profile".
  • /home/{user}/.bashrc
  • /home/{user}/.profile
File
Description
~/.bashrc
The environment variables set in this file are used for non-login Shells, such as when you open a new Terminal.
~/.profile
Environment variables set in this file are used for login Shells, such as SSH.
In most cases, it is recommended to add environment variables to "~/.profile", as it ensures the variables are available to both login and non-login Shells.
Use vim or nano commands to modify file content directly on Terminal:
sudo nano ~/.profile
Add an environment variable:
greeting="Hello everybody!"
  • Ctrl + O --> Save
  • Ctrl + X --> Exit
Note: In some versions of Ubuntu, environment variables in files are not read each time you start a new session. In that case, every time you start a new session you have to read this file manually:
source ~/.profile
No ADS
No ADS