Learn › Linux Foundations › Bash Shell Environment
printenv - a hands-on Linux lab on a real virtual machine.
Read the environment with printenv. Print the whole NAME=value list, look up one variable to get its bare value, learn why you pass the plain name and never $NAME, and read the silence-plus-exit-1 signal for a variable that is not set. printenv reads the live environment, so the full list and some values vary; the drills teach the shape and the moves.
Your shell knows things about you before you type a single command. It knows the path to your home folder. It knows your username. It knows where to hunt for programs when you type their name. All of that lives in a set of hidden labels called environment variables, and most of the time you never look at them.
But sooner or later a program misbehaves because one of those labels is wrong, and you need to READ it. Not change it, just see what it holds right now. There is a command that does exactly that, and only that. It is called printenv.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. printenv reads your REAL environment, so the full list and some values change from machine to machine. The outputs here are one honest capture, kept as an example. On the live machine at the end of the module, you run printenv against your own environment and read your own values.
printenv prints environment variables. That is its whole job. An environment variable is a named setting your shell carries and hands down to every program you launch, written as a NAME=value pair. HOME names the path to your home folder, USER names who you are logged in as, PATH names the folders searched for commands.
The name says it plainly: print the environment. Unlike some tools you will meet later, printenv never changes anything and never runs another program. It reads and it prints. That narrow focus is exactly why it is safe to reach for when you just want to know what a variable holds.
Run printenv with nothing after it and it prints every environment variable, one per line, each as a NAME=value pair. It is the fastest way to see the entire set at once.
The list is long and it is personal to your machine, so do not try to memorise it. Just run it once and watch the shape of the output:
printenv
prompt: student@linuxcamp:~$ answer: printenv output: HOME=/home/student USER=student SHELL=/bin/bash PATH=/usr/local/bin:/usr/bin:/bin LANG=C.UTF-8 PWD=/home/student hint: Just the one word and Enter, nothing after it: printenv
Every line is one variable: a name, an equals sign, then the value it holds. The real list is longer than this and its order is not sorted, so it can shuffle between runs. The exact set is yours: your machine may show extra lines, and values like the ones above will read differently on a different box. What never changes is the SHAPE, NAME=value, one per line, and that shape is what lets you read any environment at a glance.
Printing the whole list is a lot when you only care about one thing. Hand printenv a single name and it prints just that variable's value, and nothing else. No NAME=, no equals sign, just the bare value on its own line.
This is the form you will reach for most. Ask it where your home folder is by naming HOME. Note there is no dollar sign: you pass the plain name, not $HOME. Before you press Enter, decide what you expect: the whole HOME=/home/student line, or just the path?
printenv HOME
prompt: student@linuxcamp:~$ answer: printenv HOME output: /home/student hint: The word printenv, a space, then the bare name in capitals, no dollar sign: printenv HOME
Just the value, /home/student, with no HOME= in front of it. That is the difference between naming a variable and listing them all: name one and you get the clean value by itself, which is exactly what a script wants to capture. On your own machine the path may differ, but the MOVE is what matters: printenv NAME collapses the whole list down to the single value you asked for. You passed the bare name because the dollar sign is a shell thing, and here printenv does the lookup itself.
This trips up almost everyone once. You already know echo $HOME prints your home path, so it feels natural to type printenv $HOME. Do that and it fails silently, and it is worth understanding why.
The dollar sign is an instruction to the SHELL: it means expand this variable before the command runs. So the shell turns $HOME into /home/student first, and printenv receives /home/student as the name to look up. There is no environment variable literally called /home/student, so printenv finds nothing and prints an empty line. Pass the bare name, printenv HOME, and printenv does the lookup itself.
printenv $HOME prints a blank line. The shell already replaced $HOME with its value, so printenv searched for a variable named after your home path and found none. What to check first: drop the dollar sign. With printenv you always pass the plain, capitalised name, never $NAME.
Here is the behaviour that makes printenv genuinely useful, not just convenient. Ask it for a variable that is not set, and it prints nothing at all, then quietly reports failure with an exit code of 1. Empty output is not a glitch. It is the answer: that variable is not in your environment.
This has a sharp edge worth knowing. printenv only sees variables that have been exported into the environment. A variable set plainly at your shell prompt, without exporting it, is a local shell variable: echo can print it, but printenv cannot, because it never made it into the environment. So printenv doubles as a test: if a name prints nothing here, it is not truly in the environment, whatever echo might show.
Ask it for a name that does not exist and watch the silence:
printenv MADE_UP_NAME
prompt: student@linuxcamp:~$ answer: printenv MADE_UP_NAME output: hint: printenv, a space, then any name that is not set, such as MADE_UP_NAME: printenv MADE_UP_NAME
Nothing printed, and the exit code is 1. That is not a bug, it is the signal: MADE_UP_NAME is not an environment variable on this machine. What to check first: confirm the spelling and the capitals, since names are case sensitive (HOME is set, home is not). Then remember that a variable you set at the prompt without export stays local and will never show up here.
Scaffolding off. No command is printed this time. You have every piece you need.
A script needs to know who is logged in, and the environment already holds that answer under the name USER. Ask printenv for exactly that one variable, so you get the bare username on its own line and nothing else. Remember: the plain name, no dollar sign.
prompt: student@linuxcamp:~$ answer: printenv USER output: student hint: The same one-variable form you just learned, aimed at the username. Think printenv, then the bare name USER.
One line, student, the value of USER with no USER= in front. That is the everyday move: name a single variable and printenv hands back its clean value, ready to drop straight into a script. On your own machine the username will be whatever you logged in as, but the shape, one bare value, is the same everywhere. You recalled the form from memory and passed the plain name, which is the exact recall the real machine will ask of you.
You earned this cheat sheet. Every row is a form of printenv you just ran:
Two rules carry all of it. First, pass the plain name, never $NAME, because the dollar sign is the shell's job and printenv does its own lookup. Second, silence means the variable is not in the environment, which also tells you it was never exported.
printenv only READS. Its close cousin env prints the same list but can also launch a program with a changed environment, which printenv cannot. When all you want is to look a value up, printenv NAME is the cleaner, safer reach.
The practice terminal has shown you the shape of printenv. You have the full NAME=value list, the one-variable lookup that returns a bare value, the no-dollar-sign rule, and the silence-plus-exit-1 signal for a name that is not set. Every one of those you typed yourself.
The Bash Environment module ends with one real Linux machine, the Bash Environment capstone mission, and that is where you read the environment for real. A VM boots just for you, with its own live set of variables. It hands you objectives that use exactly what you practiced across this module: naming variables, reading their values, and telling exported from local. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.
Finish the other Bash Environment lessons, then go read a real environment for yourself.
Practice printenv in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.