Your current terminal session relies on specific settings stored in environment variables in Linux. These variables control everything from your shell prompt to the programs you can run. If you are new to Linux or just need a refresher, learning **how to check environment variables Linux** is a fundamental skill. This guide walks you through every method, from simple commands to advanced troubleshooting, so you can manage your system with confidence.
Environment variables are like sticky notes your system uses to remember important details. They tell your shell where to find executables, what language to use, and even how to display files. Checking them is easy once you know the right commands. Let’s jump in.
What Are Environment Variables In Linux
Before you check them, it helps to understand what they are. Environment variables are key-value pairs that influence the behavior of processes. For example, the variable `HOME` stores your home directory path, while `PATH` lists directories where the system looks for executable files.
These variables are inherited by child processes from their parent. When you open a terminal, it inherits variables from your login session. You can set, unset, or modify them temporarily or permanently. Knowing how to check them is the first step to customizing your environment.
How To Check Environment Variables Linux
The most direct way to see all environment variables is using the `env` or `printenv` command. Open your terminal and type:
“`
env
“`
This prints every variable and its value, one per line. The output can be long, so you might want to pipe it to `less` for easier reading:
“`
env | less
“`
Alternatively, use `printenv` which does the same thing:
“`
printenv
“`
Both commands show the same information. The difference is minor: `env` can also be used to run a program with a modified environment, while `printenv` is strictly for display.
Check A Specific Variable
If you only need one variable, use `echo` with the variable name preceded by a dollar sign. For instance, to see your home directory:
“`
echo $HOME
“`
Or check your current shell:
“`
echo $SHELL
“`
You can also use `printenv` with the variable name:
“`
printenv HOME
“`
This is faster than scrolling through a long list.
Using The Set Command
The `set` command shows all variables, including shell variables that are not exported as environment variables. Type:
“`
set
“`
This includes both environment and local shell variables. The output is large, so use `set | less` to browse. To filter for a specific variable, combine with `grep`:
“`
set | grep PATH
“`
This is handy when you want to see all variables related to a topic.
Common Environment Variables You Should Know
Here are some frequently used variables. Knowing them helps you understand your system better:
– `HOME`: Path to your user home directory
– `USER`: Your current username
– `SHELL`: Path to your default shell (e.g., /bin/bash)
– `PATH`: Colon-separated list of directories for executables
– `LANG`: System language and locale settings
– `PWD`: Current working directory
– `OLDPWD`: Previous working directory
– `TERM`: Terminal type (e.g., xterm-256color)
To check any of these, use `echo $VARIABLE_NAME`.
How To Check Environment Variables In Different Shells
Most Linux systems use Bash by default, but you might use Zsh, Fish, or others. The commands are similar, but there are small differences.
Bash
In Bash, `env`, `printenv`, and `set` all work. You can also use `declare -p` to see variable attributes:
“`
declare -p HOME
“`
Zsh
Zsh supports the same commands. Additionally, you can use `typeset`:
“`
typeset -x
“`
This shows exported variables.
Fish
Fish uses a different syntax. To list all environment variables:
“`
set -x
“`
Or check a specific one:
“`
echo $HOME
“`
Fish also has a nice `env` command that works the same.
How To Check Environment Variables For A Running Process
Sometimes you need to see what variables a running program is using. The `/proc` filesystem holds this information. Find the process ID (PID) first with `ps` or `pgrep`. For example, to check a Bash process:
“`
pgrep bash
“`
Then look at its environment:
“`
cat /proc/PID/environ
“`
Replace `PID` with the actual number. The output is null-separated, so use `tr` to make it readable:
“`
cat /proc/PID/environ | tr ‘\0’ ‘\n’
“`
This is useful for debugging or security audits.
How To Check Environment Variables In Scripts
When writing shell scripts, you often need to verify variables. Use the same commands inside the script. For example:
“`bash
#!/bin/bash
echo “Home is: $HOME”
echo “All variables:”
env
“`
You can also check if a variable exists:
“`bash
if [ -z “$MY_VAR” ]; then
echo “MY_VAR is not set”
fi
“`
This prevents errors when your script depends on specific settings.
How To Check Environment Variables Remotely
If you manage multiple servers, you might need to check variables over SSH. Use:
“`
ssh user@server ‘env | grep PATH’
“`
Or run a full check:
“`
ssh user@server ‘printenv’
“`
This is efficient for remote administration.
How To Check Environment Variables With Graphical Tools
While the terminal is fastest, some desktop environments offer graphical tools. For example, on GNOME, you can open the “Environment Variables” dialog from settings. On KDE, use `systemsettings` and look for “Environment Variables”. These are less common but helpful for beginners.
How To Check Environment Variables For Debugging
When something breaks, environment variables are often the culprit. Here is a systematic way to debug:
1. Check if the variable is set: `echo $VARIABLE`
2. Verify the value is correct: `printenv VARIABLE`
3. Compare with expected values from documentation
4. Look for typos in variable names
5. Check if the variable is exported: `declare -x VARIABLE`
For PATH issues, print it with each directory on a new line:
“`
echo $PATH | tr ‘:’ ‘\n’
“`
This makes it easy to spot missing directories.
How To Check Environment Variables In Cron Jobs
Cron jobs run with a limited environment. To see what variables are available, add this to your crontab:
“`
* * * * * env > /tmp/cron_env.txt
“`
Wait a minute, then check the file. You will notice many variables are missing compared to your interactive shell. This is a common source of script failures.
How To Check Environment Variables In Systemd Services
Systemd services also have a restricted environment. To inspect a service’s environment:
“`
systemctl show SERVICE_NAME | grep Environment
“`
Or for a running service:
“`
cat /proc/$(systemctl show –property MainPID SERVICE_NAME | cut -d= -f2)/environ | tr ‘\0’ ‘\n’
“`
This helps when debugging service failures.
How To Check Environment Variables In Containers
Docker and Podman containers have their own environments. To check inside a running container:
“`
docker exec CONTAINER_NAME env
“`
Or for a specific variable:
“`
docker exec CONTAINER_NAME echo $VARIABLE
“`
For images, you can inspect the environment during build with `docker history`.
How To Check Environment Variables In Python Or Other Languages
If you are writing scripts in Python, you can access environment variables via `os.environ`:
“`python
import os
print(os.environ.get(‘HOME’))
“`
In Perl:
“`perl
print $ENV{‘HOME’};
“`
In Node.js:
“`javascript
console.log(process.env.HOME);
“`
This is useful when your application needs to read system settings.
How To Check Environment Variables With Aliases And Functions
To speed up your workflow, create aliases or functions. Add these to your `.bashrc`:
“`bash
alias envs=’env | sort’
function checkvar() {
echo “${1}=${!1}”
}
“`
Now `envs` shows sorted variables, and `checkvar HOME` prints the value. This saves time.
Common Mistakes When Checking Environment Variables
Here are pitfalls to avoid:
– Forgetting the dollar sign: `echo HOME` prints the word “HOME”, not its value
– Using spaces incorrectly: `echo $ HOME` will break
– Confusing shell variables with environment variables: `set` shows both, `env` shows only exported ones
– Not quoting variables with spaces: `echo $PATH` works, but `echo “$PATH”` is safer
Always quote variables when using them in scripts to prevent word splitting.
How To Check Environment Variables In Different Linux Distributions
The commands are standard across distributions, but default variables differ. For example, Ubuntu sets `DEBIAN_FRONTEND`, while Fedora sets `SYSTEMD_*`. Use `env | grep -i distro` to find distribution-specific variables.
How To Check Environment Variables In A Non-Interactive Shell
When you run a script via `sh script.sh`, the shell is non-interactive. Environment variables may differ. To see them, add `env` at the start of your script. This helps when debugging cron or systemd issues.
How To Check Environment Variables With One-Liners
Here are some useful one-liners:
– Count variables: `env | wc -l`
– Search for a pattern: `env | grep -i path`
– Export variables to a file: `env > backup.txt`
– Compare two environments: `diff <(env) <(ssh server env)`
These are handy for automation.
How To Check Environment Variables In A Chroot Or Jail
When working inside a chroot, environment variables are inherited from the host. To check, just run `env` inside the chroot. If you need a clean environment, use `env -i` to start with an empty environment.
How To Check Environment Variables For Security Audits
Security professionals often check for sensitive variables like `AWS_SECRET_ACCESS_KEY` or `DB_PASSWORD`. Use:
“`
env | grep -iE ‘secret|password|key|token’
“`
If you find any, consider removing them or using a secrets manager.
How To Check Environment Variables In A Minimal System
On embedded systems or minimal containers, `env` might not be installed. Use `cat /proc/1/environ` as a fallback. This works even with busybox.
How To Check Environment Variables With A GUI File Manager
Some file managers like Nautilus show environment variables in the properties dialog. Right-click a file, go to Properties, and look for “Environment”. This is rare but exists in some desktop environments.
How To Check Environment Variables In A Tmux Or Screen Session
Tmux and Screen preserve environment variables from the parent shell. To check inside a session, just run `env`. If you need to update them, use `tmux set-environment`.
How To Check Environment Variables In A Subshell
A subshell inherits the parent’s environment. To verify, run:
“`
(env | grep HOME)
“`
This is useful for testing variable propagation.
How To Check Environment Variables In A Pipeline
Pipelines run in subshells, so variables set in a pipeline are not visible outside. To check pipeline environment, use `echo` inside the pipeline:
“`
echo $HOME | cat
“`
This prints the variable value.
How To Check Environment Variables In A Debugger
If you use `gdb` or `strace`, you can inspect environment variables. For `strace`:
“`
strace -e trace=execve command
“`
This shows the environment passed to executed programs.
How To Check Environment Variables In A Log File
Many applications log their environment at startup. Check `/var/log/syslog` or application-specific logs for lines containing “env” or “environment”.
How To Check Environment Variables In A Web Server
For Apache or Nginx, environment variables are often set in configuration files. Check `/etc/apache2/envvars` or `/etc/nginx/fastcgi_params`. You can also use `phpinfo()` in PHP to see server variables.
How To Check Environment Variables In A Database
Some databases like PostgreSQL read environment variables for configuration. Check `SHOW ALL;` in psql to see which ones are used.
How To Check Environment Variables In A Cloud Instance
Cloud providers like AWS set metadata variables. On EC2, you can check `http://169.254.169.254/latest/meta-data/` but these are not standard environment variables. Use `env` to see what’s set.
How To Check Environment Variables In A Virtual Machine
Inside a VM, environment variables are the same as on a physical machine. Use the same commands. The hypervisor may set variables like `VMWARE_GUEST`.
How To Check Environment Variables In A Recovery Environment
When booting from a live USB or recovery mode, environment variables are minimal. Use `env` to see what’s available. This helps when fixing a broken system.
How To Check Environment Variables In A Build System
CI/CD systems like Jenkins or GitLab CI set many variables. To check them in a build step, add `env` to your script. For example, in a `.gitlab-ci.yml`:
“`yaml
script:
– env
“`
This prints all variables during the build.
How To Check Environment Variables In A Test Suite
When running tests, environment variables can affect outcomes. Use `env` at the start of your test script to log the environment. This helps reproduce failures.
How To Check Environment Variables In A Production Environment
In production, be careful not to expose sensitive variables. Use `env | grep -v SECRET` to filter. Or use a dedicated tool like `envcheck` from your package manager.
How To Check Environment Variables In A Multi-User System
Each user has their own environment. To check another user’s variables, use `sudo -u username env`. This requires root privileges.
How To Check Environment Variables In A Cluster
On a cluster, environment variables are often set by the job scheduler. Use `env` inside your job script to see what’s available. For SLURM, check `SLURM_*` variables.
How To Check Environment Variables In A Kubernetes Pod
Inside a pod, environment variables are set from the pod spec. Use `kubectl exec POD_NAME — env` to check them. This is essential for debugging containerized applications.
How To Check Environment Variables In A Serverless Function
AWS Lambda or Google Cloud Functions set environment variables from the function configuration. Use `process.env` in Node.js or `os.environ` in Python to check them in your code.
How To Check Environment Variables In A Legacy System
On older Unix systems, `env` might not exist. Use `printenv` or `set`. On very old systems, check `/etc/environment` or `/etc/profile`.
How To Check Environment Variables In A Non-English Locale
Locale variables like `LANG` affect output. To see them, use `locale` command. This prints all locale-related variables.
How To Check Environment Variables In A Custom Shell
If you use a custom shell like `dash` or `mksh`, the commands are the same. Check the shell’s documentation for any differences.
How To Check Environment Variables In A Restricted Shell
A restricted shell like `rbash` limits what you can do. `env` usually works, but `set` might be disabled. Use `printenv` as an alternative.
How To Check Environment Variables In A Sandbox
Sandboxed environments like Firejail or Bubblewrap have restricted variables. Use `firejail –env=HOME=/tmp env` to test.
How To Check Environment Variables In A Container Runtime
For containerd or CRI-O, use `crictl exec CONTAINER_ID env`. This is similar to Docker but uses different tooling.
How To Check Environment Variables In A Virtual Environment
Python virtual environments set `VIRTUAL_ENV`. Check it with `echo $VIRTUAL_ENV`. This tells you if a venv is active.
How To Check Environment Variables In A Development Environment
Developers often set variables like `DEBUG` or `NODE_ENV`. Use `env | grep -E ‘DEBUG|NODE|DEV’` to find them quickly.
How To Check Environment Variables In A Testing Environment
Testing environments may set `CI`, `TEST`, or `TRAVIS` variables. Use `env | grep -i test` to see them.
How To Check Environment Variables In A Staging Environment
Staging environments often mirror production but with different values. Use `env` to compare with production.
How To Check Environment Variables In A Disaster Recovery Environment
In DR scenarios, environment variables might be minimal. Use `env` to verify critical paths like `PATH` and `HOME` are set correctly.
How To Check Environment Variables In A Backup Environment
Backup scripts often use variables like `BACKUP_DIR`. Check them with `echo $BACKUP_DIR` before running the script.
How To Check Environment Variables In A Monitoring Environment
Monitoring tools like Nagios or Prometheus set variables. Check `/etc/default/nagios` or use `env` in a check