Quick Answer

# Temporary (current session only)
export MY_VAR="hello"

# Persistent (survives reboot and new terminals)
echo 'export MY_VAR="hello"' >> ~/.bashrc
source ~/.bashrc

What You’re Trying to Do

You need to set an environment variable in Linux, but it disappears when you close the terminal. Or you want a tool or application to always have access to a specific variable without setting it every time.


Environment

  • OS: Ubuntu 22.04 / Debian 12 (works on most Linux distributions)
  • Shell: bash / zsh

Solution

1. View existing environment variables

# List all environment variables
env

# Check a specific variable
echo $HOME
echo $PATH
echo $MY_VAR

2. Set a temporary environment variable (current session only)

export MY_VAR="hello"
echo $MY_VAR
# → hello

This is lost when the terminal is closed.

3. Persist using .bashrc (per-user)

echo 'export MY_VAR="hello"' >> ~/.bashrc
source ~/.bashrc

.bashrc is loaded for every interactive shell session.

4. Use .bash_profile for login shells

echo 'export MY_VAR="hello"' >> ~/.bash_profile
source ~/.bash_profile

Loaded on SSH logins and initial login sessions.

5. System-wide variables using /etc/environment

sudo nano /etc/environment

Add variables in the following format (no export keyword):

MY_VAR="hello"
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Requires a re-login or reboot to take effect.

6. Unset an environment variable

unset MY_VAR
echo $MY_VAR
# → (empty)

7. Set a variable for a single command

MY_VAR="hello" node app.js

This sets the variable only for that process and does not affect the current shell.


Common Errors

export: not valid in this context

export: 'MY_VAR=hello world' is not valid in this context

Cause: The value contains spaces but is not quoted.
Fix:

export MY_VAR="hello world"

source: command not found

Occurs when running a script with sh instead of bash.
Fix: Use . instead of source (they are equivalent):

. ~/.bashrc

Environment variable disappears after reboot

If you added it to .bashrc but it still disappears, your login shell may only read .bash_profile.
Fix: Call .bashrc from .bash_profile:

# Add to ~/.bash_profile
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

PATH is broken — ls and other commands not found

# Temporarily repair PATH for the current session
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

Then open .bashrc and fix the broken PATH entry.


FAQ

Q: What is the difference between export VAR=value and VAR=value?
VAR=value sets the variable only in the current shell. export VAR=value also makes it available to child processes (scripts and commands you run from that shell).

Q: I use zsh — which file should I edit?
Add your exports to ~/.zshrc for persistent variables in zsh. For login shells, use ~/.zprofile or ~/.zlogin.

Q: What is the difference between env and printenv?
Both list environment variables. printenv VAR prints a single variable’s value. env can also run a command in a modified environment.

Q: Should I use .bashrc or .bash_profile?
Use .bashrc for variables you want in every terminal session. Use .bash_profile for variables that only need to be set once at login (e.g., on SSH). For most use cases, .bashrc is the right choice.

Q: How do I set an environment variable for all users?
Edit /etc/environment (no export keyword needed). Alternatively, add a script under /etc/profile.d/ — every file there is sourced at login.

Q: Can Node.js and Python read these environment variables?
Yes. Variables set with export are accessible via process.env.MY_VAR in Node.js and os.environ['MY_VAR'] in Python.


If you want to build a production Linux environment, these VPS services are a great starting point: