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.shRemove a variable with unset APP_.
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_ 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
| File | Format |
|---|---|
/etc/ | NAME= lines only |
/etc/ | Shell script with export |
/etc/environment is not a script. The PAM module pam_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.serviceIn 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.envRun 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 EnvironmentFilesThe 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_ default), and the default sudoers file on Ubuntu, Debian, AlmaLinux, and Rocky Linux also sets secure_, 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_, if the sudo policy allows it.ENV command - Keep all variables:
sudo -E command, if the policy allows it. Wheresecure_is set,path PATHis 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.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations