How To Print Env Variable In Linux – Shell Variable Display Command

Printing an environment variable in Linux uses the echo command followed by a dollar sign and the variable name. If you’ve ever wondered how to print env variable in linux, you’re in the right place. This guide covers everything from basic commands to advanced tricks, all in simple steps.

Environment variables store system and user settings. They control things like your shell, home directory, and program paths. Knowing how to print them helps you debug, configure software, and understand your system better.

Let’s start with the most common method. Open your terminal. Type echo $VARIABLE_NAME and press Enter. Replace VARIABLE_NAME with the actual variable, like echo $HOME. You’ll see the value printed instantly.

This works for any variable. Try echo $USER to see your username, or echo $SHELL to check your default shell. It’s that simple.

How To Print Env Variable In Linux

Now we dive deeper. The echo command is your go-to tool, but there are other ways to print environment variables. Each method has its own use case.

Using The Printenv Command

printenv is a dedicated command for printing environment variables. Type printenv alone to list all variables. To print a specific one, use printenv VARIABLE_NAME.

For example, printenv PATH shows your system’s search paths. This command is cleaner than echo when you need a quick list.

Using The Env Command

env also prints environment variables. Running env without arguments lists everything. To filter, pipe it to grep: env | grep PATH.

Unlike printenv, env can run a program with modified environment. But for printing, it’s similar. Use it when you need to combine with other commands.

Using Variable Expansion In Bash

Bash offers variable expansion with ${VARIABLE_NAME}. This is useful when you need to embed the variable in a string. For example, echo "My home is ${HOME}".

This method prevents ambiguity. If you have a variable named HOME_DIR, echo $HOME_DIR works, but echo ${HOME}_DIR prints the value of HOME followed by “_DIR”.

Common Environment Variables To Print

Here are variables you’ll frequently check:

  • HOME – Your home directory
  • USER – Current username
  • SHELL – Default shell path
  • PATH – Executable search paths
  • LANG – System language and locale
  • PWD – Current working directory
  • OLDPWD – Previous working directory
  • TERM – Terminal type

Print any of these with echo $VARIABLE. For instance, echo $PWD shows where you are right now.

Printing All Environment Variables

Sometimes you need to see everything. Use these commands:

  1. printenv – Lists all variables
  2. env – Also lists all variables
  3. set – Lists all variables, including shell functions

The set command shows more than environment variables. It includes local variables and functions. To see only environment variables, stick with printenv or env.

Filtering Output With Grep

When you have many variables, pipe to grep. For example, env | grep -i path finds any variable containing “path” (case-insensitive).

This is handy for debugging. If you’re looking for a variable but forgot its exact name, env | grep -i keyword helps you find it.

Printing Variables In Scripts

In bash scripts, you print variables the same way. Use echo "$VARIABLE" inside double quotes to preserve spaces and special characters.

Example script:

#!/bin/bash
echo "User: $USER"
echo "Home: $HOME"
echo "Shell: $SHELL"

Run it with bash script.sh. Each line prints the variable value.

Using Printf For Formatted Output

printf gives you more control. Use printf "%s\n" "$VARIABLE" to print a variable with a newline. This is useful for formatting in scripts.

For example, printf "Your home is %s\n" "$HOME" prints a clean message.

Debugging With Variable Printing

Printing variables helps debug scripts. If a script doesn’t work, add echo "Variable is: $VAR" to see its value at different points.

Common issues include unset variables. Use echo "${VAR:-default}" to print a default if the variable is empty.

Checking If A Variable Is Set

Use test -v VARIABLE or [[ -v VARIABLE ]] to check if a variable exists. Then print it conditionally.

Example: if [[ -v MYVAR ]]; then echo "$MYVAR"; else echo "Not set"; fi

Printing Variables From Other Processes

You can print environment variables of a running process. Use cat /proc/PID/environ where PID is the process ID. This shows all variables for that process.

To read it cleanly, use tr '\0' '\n' < /proc/PID/environ. This replaces null characters with newlines.

Finding A Process ID

Use pgrep or ps to find PID. For example, pgrep bash gives the PID of your bash shell. Then check its environment.

Persisting Variable Changes

When you set a variable in a terminal, it's temporary. To make it permanent, add it to your shell's config file like ~/.bashrc or ~/.profile.

After editing, run source ~/.bashrc to reload. Then print the variable to confirm it's set.

Common Mistakes When Printing Variables

Here are pitfalls to avoid:

  • Forgetting the dollar sign: echo HOME prints "HOME", not its value
  • Using single quotes: echo '$HOME' prints literally "$HOME"
  • Spaces around equals: VAR = value is wrong; use VAR=value
  • Case sensitivity: echo $home won't work if the variable is HOME

Always use double quotes when printing variables with spaces: echo "$MYVAR".

Advanced Printing Techniques

Using Awk Or Sed

You can extract variable values from output. For example, env | awk -F= '/^PATH/ {print $2}' prints only the PATH value.

This is useful in scripts when you need to manipulate the value.

Printing Variables In Different Shells

Bash, Zsh, and Fish all support echo $VAR. But syntax differs slightly. In Fish, you use echo $VAR without quotes for arrays.

Check your shell with echo $SHELL. Then adjust commands accordingly.

Using Environment Variables In Commands

You can print variables directly in command arguments. For example, cd $HOME changes to your home directory. Or ls -l $HOME/Downloads lists files there.

This makes your commands dynamic and reusable.

Exporting Variables For Subprocesses

If you set a variable without export, it's local to the shell. Use export MYVAR=value to make it available to child processes. Then print it in a subshell with echo $MYVAR.

Printing Variables With Special Characters

Variables can contain spaces, tabs, or newlines. Use double quotes to preserve them: echo "$MYVAR".

For newlines, echo might not show them correctly. Use printf '%s\n' "$MYVAR" instead.

Automating Variable Printing

Create an alias to quickly print common variables. Add to ~/.bashrc:

alias myenv='echo "User: $USER, Home: $HOME, Shell: $SHELL"'

Then type myenv to see all three at once.

Using Functions

Write a function to print multiple variables:

function print_vars() {
  echo "PATH: $PATH"
  echo "HOME: $HOME"
  echo "USER: $USER"
}

Call it with print_vars.

Security Considerations

Environment variables can contain sensitive data like API keys. Be careful when printing them in logs or sharing output.

Use set +o history before printing secrets to avoid saving them in command history. Or use history -c to clear it.

Troubleshooting Common Issues

If a variable doesn't print, check these:

  • Is it set? Use echo ${VAR:-unset}
  • Is it exported? Use export -p | grep VAR
  • Is the name correct? Case matters
  • Are you in the right shell? Different shells have different variables

For system-wide variables, check /etc/environment or /etc/profile.

Printing Variables In Cron Jobs

Cron jobs run with a minimal environment. To print variables in a cron script, set them explicitly at the top:

#!/bin/bash
HOME=/home/user
PATH=/usr/bin:/bin
echo "Home is $HOME"

This ensures your script works as expected.

Using Variable Printing For Learning

Printing variables is a great way to learn Linux. Experiment with echo $RANDOM to see a random number, or echo $SECONDS for shell uptime.

These built-in variables teach you about the system.

Conclusion

Printing environment variables in Linux is straightforward. Use echo $VAR for quick checks, printenv for lists, and env for filtered output. Master these commands to debug scripts, configure software, and understand your system.

Practice with different variables. Try echo $HOSTNAME, echo $OSTYPE, or echo $BASH_VERSION. Each reveals something about your environment.

Now you know how to print env variable in linux. Go ahead and explore your system's settings.

Frequently Asked Questions

How Do I Print All Environment Variables In Linux?

Use printenv or env to list all environment variables. For a filtered list, pipe to grep like env | grep PATH.

What Is The Difference Between Echo And Printenv?

echo $VAR prints one variable at a time. printenv can print all variables or a specific one. printenv is more readable for lists.

Can I Print A Variable That Is Not Exported?

Yes, if the variable is set in the current shell (without export), echo $VAR works. But it won't be available to subprocesses.

Why Does Echo $HOME Not Work In A Script?

Scripts run in a subshell. If HOME is not exported, it may be empty. Use export HOME or set it in the script.

How Do I Print A Variable With Spaces?

Use double quotes: echo "$MYVAR". This preserves spaces and special characters.