Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Set a Variable Now

export APP_ENV=production
echo "$APP_ENV"

The variable exists until you close this shell. Use quotes if the value has spaces: export GREETING="hello world". To set a variable for one command only, put it in front of the command:

APP_ENV=staging ./deploy.sh

Remove a variable with unset APP_ENV.

Shell Variable or Environment Variable?

Without export, NAME=value creates a shell variable that only the current shell sees. export marks it to be passed on to every program the shell starts. A script cannot change the environment of the shell that ran it; to load variables from a file into your current shell, use source file.sh.

See Variables

printenv
printenv PATH
env | grep APP_

printenv NAME prints one value and returns a non-zero exit code if the variable is not set, which makes it usable in scripts.

Make It Permanent for Your User

Add the export line to a shell startup file in your home folder:

echo 'export APP_ENV=production' >> ~/.bashrc
source ~/.bashrc

~/.bashrc is read by interactive Bash shells that are not login shells. Login shells, such as an SSH session, read ~/.profile on Ubuntu and Debian or ~/.bash_profile on AlmaLinux and Rocky Linux; the default versions of those files read ~/.bashrc too, so one line there covers both.

Make It Permanent for All Users

FileFormat
/etc/environmentNAME=value lines only
/etc/profile.d/app.shShell script with export

/etc/environment is not a script. The PAM module pam_env reads its plain NAME=value lines when a session starts, such as an SSH login, so log out and in again to see the change. An export word is ignored there, and you should not rely on $VAR references being expanded. For anything that needs shell syntax, create a file in /etc/profile.d/ ending in .sh.

Add a Folder to PATH

export PATH="$HOME/bin:$PATH"

Keep $PATH in the new value, or the shell loses every other folder and common commands stop working. Put your folder first to take priority over system commands, or last to be searched after them. Add the line to ~/.bashrc to keep it.

Variables for systemd Services

A service started by systemd does not read ~/.bashrc or /etc/profile.d/. Set its variables in a drop-in file:

sudo systemctl edit app.service

In the editor, add:

[Service]
Environment="APP_ENV=production"
Environment="APP_PORT=8080"

For many values or for secrets, keep them in a separate file and protect it:

[Service]
EnvironmentFile=/etc/app/app.env

Run sudo chmod 600 /etc/app/app.env on the root-owned file so only root can read it; systemd reads it when the service starts. systemctl edit reloads the unit configuration when you save, but the running service keeps its old values until you restart it:

sudo systemctl restart app.service
systemctl show app.service -p Environment -p EnvironmentFiles

The first property lists the Environment= lines; the second names the files, whose values are not printed.

Containers work in a similar way; Docker passes variables with -e or --env-file, as covered in how to install Docker on Ubuntu.

sudo and the Environment

sudo starts commands with a reduced environment (the env_reset default), and the default sudoers file on Ubuntu, Debian, AlmaLinux, and Rocky Linux also sets secure_path, which replaces PATH with a fixed list of system folders. That is why a variable you exported, or a program in ~/bin, can seem to disappear under sudo.

  • Keep one variable: sudo --preserve-env=APP_ENV command, if the sudo policy allows it.
  • Keep all variables: sudo -E command, if the policy allows it. Where secure_path is set, PATH is still replaced.
  • Set it for the command: sudo env APP_ENV=production command.

Cron jobs also start with a minimal environment, so set the variables a cron script needs inside the script. The basics of scripts are in how to write a Bash script, and checking whether a variable is empty is shown in the Bash if statement guide.

Was this answer helpful?
Loading...