How to Print Environment Variables in Linux
Think of environment variables in Linux like little notes or settings that the system and programs use to know how to behave. They’re like behind-the-scenes instructions. For example: Wanna know where your home folder is? There’s a variable for that. Want to tell the system where to look for programs when you type commands? Yep, there’s a variable for that too. They’re kind of like settings in a video game — stuff you don’t always see, but it controls how things work.
Let’s say you type a command like python in your terminal. How does Linux know where to find Python?
It looks at the PATH environment variable — a list of folders where your system checks for programs to run. If Python isn’t in one of those folders, it’ll just say “command not found.”\
Open your terminal and type:
echo $HOME
That’ll show your home directory — that’s an environment variable called HOME. Try:
echo $USER
That’ll show your username. These variables start with a $ when you want to use them.
You can also create your own. Like this:
export MYNAME=”Musk”
echo $MYNAME
Now you’ve created your own environment variable! But it’ll only last while your terminal is open. Close it, and it’s gone. Want it to stick around every time you open a terminal? You can add that line to your ~/.bashrc or ~/.zshrc file.
How to Print Environment Variables in Linux
Before jumping into the main section, let’s have a look into the common environment variables in Linux.
Common environment variables in Linux
| Environment Variable | Description |
|---|---|
| HOME | Shows home directory for the current user |
| HOSTNAME | Contains Hostname of system |
| UID | Stores unique ID of the users |
| SHELL | Shows the path to the shell that is being used currently |
| BASH_VERSION | Contains version currently used bash instance in system |
| HISTFILE | Path of the file where the history of commands is saved in your system |
| TERM | Shows the type of log-in terminal you’re using in your current system |
| PATH | Shows path of commands and directories are separated by columns |
There are various ways through which you can print environment variables in Linux. Let’s break it down in a simple way! If you want to see the environment variables in Linux, there are a few different ways you can do it.
1. Using printenv Command
The printenv command is probably the easiest way to see environment variables. The printenv utility is used to print environment variables for the current shell.
To see all variables:
printenv
To see a specific variable:
printenv HOME
It will just print the value of that environment variable. So, if you run printenv HOME, it’ll show your home directory.
2. Using echo Command
You can use echo to print the value of any specific environment variable. You need to use the $ symbol before the variable name
Example:
echo $HOME
This will print your home directory. If you want to see other variables, like the current user:
echo $USER
3. Using env Command
The env command lists all the environment variables currently set in your session, similar to printenv.
To see all variables:
env
It’s just another way to check the environment variables that are available to your session in your Linux.
4. Using set Command
If you’re in a shell (like bash or zsh), the set command shows all the variables and functions that are available to the shell. This includes environment variables, but also local variables.
To see all variables (including environment and shell-specific ones):
set
This command gives you a lot of information, not just environment variables. It’s a bit more comprehensive but can get messy if you just want the environment variables.
5. Using declare Command
The declare command is used to declare and print the value of variables in the shell.
You can use declare without any argument and will bring the list of available variables.
declare
Discover the thrill of True Fortune Casino UK with exclusive offers, bonuses, and free spins that keep you spinning and winning.
In the UK, true fortune casino stands out as a leading hub for players seeking genuine thrills and dependable service.
New users benefit from an easy registration flow and swift payment processing.
A rich assortment of games awaits, spanning slots, classic table games, and real-time hosts.
Security is a priority with advanced encryption and regular audits ensuring fair play.
true fortune https://true-fortune-uk.com/
Customer support operates 24/7 to assist with deposits, withdrawals, and gaming inquiries.
The platform accepts multiple payment methods, making it convenient to fund accounts.
Ongoing promotions provide value, including welcome bonuses and loyalty rewards.