Terminal Environment
Objectives
By the end of this chapter, you should be able to:
- Describe what a terminal environment is
- Create and use environment variables, including reading the
PATH - Save an environment variable so it survives closing your terminal
๐ก Why this matters: Environment variables quietly control how your shell behaves โ which programs it can find, what your prompt looks like, where temporary files go. When a command mysteriously “isn’t found,” or a setting resets every time you open a new window, this is almost always why.
What Is Your Environment?
Your terminal’s environment is a list of settings that programs can read. Run env right now and you’ll see a long list like this:
TERM=xterm-256color
SHELL=/bin/zsh
USER=sam
PWD=/Users/sam/Projects/practice
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
...
Everything to the left of each = is a variable name; everything to the right is its value.
Reading a Variable
echo, with a $ in front of the variable name, prints its value:
echo $PWD
That should match whatever pwd on its own prints โ PWD is just the environment’s copy of your current directory.
Creating Your Own Variable
Make a folder to work with:
mkdir ~/Projects
Now define a variable that points at it, using export:
export PROJDIR=/Users/sam/Projects
Notice there’s no $ when you define a variable โ only when you read one. Try it out:
cd ~
cd $PROJDIR
That should drop you straight into ~/Projects. One catch: close this terminal window and the variable is gone. Every new shell session starts with a fresh environment.
Making It Permanent
To keep a variable around across sessions, add it to your shell’s configuration file โ ~/.zshrc for Zsh, ~/.bash_profile for Bash. Open that file and add:
export PROJDIR=/Users/$USER/Projects
Save it, quit every terminal window, and reopen one. echo $PROJDIR should now print the path without you having to redefine anything.
Notice $USER inside that line โ a variable can reference another variable. That means you can build on variables you’ve already defined:
export PYTHON_DIR=$PROJDIR/python
The only rule: the line defining PROJDIR has to come before the line that uses it, or PROJDIR won’t exist yet when PYTHON_DIR is being set.
The PATH Variable
PATH is the environment variable your shell uses to find programs to run. To see what happens when it’s empty, try this in a fresh terminal window (so you don’t lose your working PATH permanently):
export PATH=
Now try ls. It fails โ along with man, chgrp, and almost everything else. That’s because commands like ls are just program files sitting somewhere on disk, and without PATH, your shell has no idea where to look.
Open a new terminal window to get a working PATH back, then check where ls actually lives:
which ls
That’s usually /bin/ls. Now try narrowing PATH down to just that folder:
export PATH=/bin
ls still works โ /bin is on the PATH again โ but man doesn’t, since it lives elsewhere. Rather than overwriting PATH completely, you can extend it, using $PATH to keep what’s already there and a colon to separate entries:
export PATH=$PATH:/usr/bin:/usr/sbin:/sbin
man works again, and echo $PATH shows every folder your shell now searches, colon-separated. Once you’re done experimenting, close this window and open a fresh one so your real PATH is back to normal.
Try It
Environment variables are shell state, not files โ there’s no sandbox for this, so work through these in your own terminal:
- Create a variable called
MY_NAMEset to your name, then print it. - Add a line to your shell config file that sets a variable permanently, then confirm it survives a fresh terminal window.
- Run
whichon two or three commands you use often (ls,cat,grep) and note which folder each one comes from.
Recap
- The environment is a list of
NAME=valuesettings your shell and programs can read. - Define a variable with
export NAME=value; read one withecho $NAME. - A variable set in a terminal window disappears when you close it โ add it to
~/.zshrcor~/.bash_profileto make it permanent. PATHis the list of folders your shell searches for programs; break it and evenlsstops working.
Next lesson: what a process actually is, and how to see โ and stop โ one from Terminal.