Environment Variables & Shell Config
The shop's app reads its settings from environment variables, but it keeps starting in the wrong mode, its admin tool says "command not found", and the database password is sitting in a script. You will configure the app the way it is done in real deployments, from the shell to a protected .env file.
- Explain the difference between shell variables and exported environment variables
- Read and change PATH, and find where a command comes from with which and type
- Make variables and aliases permanent, per user (.bashrc) and for everyone (/etc/profile.d)
- Load configuration from a .env file and keep secrets out of scripts and Git
Every process carries a set of named values: its environment. HOME, USER, PATH, LANG and many more tell programs who is running them and how to behave. Applications use the same mechanism for their settings, which is why you will meet environment variables in Docker (-e, --env-file), Kubernetes (env:, ConfigMaps, Secrets), CI pipelines and the "twelve-factor app" rule: configuration lives in the environment, not in the code.
| Kind | Created with | Seen by programs you start? |
|---|---|---|
| Shell variable | NAME=value | No, only by this shell |
| Environment variable | export NAME=value | Yes, copied to every child process |
| For one command | NAME=value command | Only by that one command |
Two rules to remember: there are no spaces around =, and a child process can never change its parent's variables.
The shop's files are in /opt/shop: app.sh (the app), start.sh (the proper launcher), bin/shopctl (an admin tool) and deploy-old.sh (the old, insecure launcher).
- Press Start lab. You get your own Ubuntu machine on the right, just for you.
- Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
- Do the Your task part, then press Check my work. The check looks at the machine and tells you what is right or still missing.
- Stuck? Open the hint. Nothing you do here can break anything real.
Variables and export
Start the shop app in production mode with the right database host.
Why we do this: the same code runs in development, staging and production. Environment variables are what tell it which one it is. A forgotten export is a classic reason an app "ignores" a setting you are sure you set.
1. Look at your environment
env | sort | head -20
echo "$HOME $USER $SHELL"
printenv PATHenv and printenv list the exported variables. echo $NAME prints one value.
2. A shell variable is not enough
cat /opt/shop/app.sh
APP_ENV=staging
echo "$APP_ENV"
/opt/shop/app.shYour shell knows APP_ENV, but the app prints APP_ENV=unset. A plain variable stays inside the current shell, and the app runs as a separate child process.
3. export makes it visible to child processes
export APP_ENV=staging
/opt/shop/app.shNow the app sees it. Check with env | grep APP_.
4. For one command only
APP_ENV=test /opt/shop/app.sh
echo "$APP_ENV"The app ran in test mode, but your shell still has staging. This form is common in scripts and Makefiles.
5. Remove a variable
unset APP_ENV
/opt/shop/app.shEvery run is also recorded in the log: cat /var/log/shop-app.log.
Start /opt/shop/app.sh so that it runs with APP_ENV=production and DB_HOST=db.internal.
Expected result: the last line of /var/log/shop-app.log shows APP_ENV=production DB_HOST=db.internal.
Hint: the commands, explained
export APP_ENV=production DB_HOST=db.internal
/opt/shop/app.shWhat each part does
export NAME=value: create an environment variable in this shell and pass it to every program you start from it. Two variables can be set in one line.- Then run the app: it reads
APP_ENVandDB_HOSTfrom its environment and starts with them.
PATH and "command not found"
Make the shopctl admin tool work in every new shell, without breaking other commands.
Why we do this: "command not found" usually doesn't mean a program is missing. It means its folder is not in PATH. Getting PATH wrong is also how people accidentally break every command in their shell, so it's worth doing carefully.
1. How the shell finds commands
echo "$PATH"
echo "$PATH" | tr ':' '\n'PATH is a list of folders separated by :. When you type ls, the shell looks in each folder in order and runs the first ls it finds.
which ls
type ls cd llwhich shows the file that would run. type also knows about shell built-ins (cd), aliases (ll) and functions, so it's the more honest answer.
2. The admin tool isn't found
shopctl status
ls -l /opt/shop/bin/
/opt/shop/bin/shopctl statusThe tool exists and works with its full path, but /opt/shop/bin is not in PATH.
3. Add a folder to PATH
export PATH="$PATH:/opt/shop/bin"
shopctl statusAlways include $PATH itself. export PATH=/opt/shop/bin on its own would replace the list, and suddenly ls, cat and vim are all "not found". (If that happens, run export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin to recover.)
Putting the folder at the start (/opt/shop/bin:$PATH) means its commands win over system ones with the same name. At the end is the safer default.
4. Make it permanent
The export above only lasts until you close this shell. To have it in every new interactive shell for root, add it to ~/.bashrc:
echo 'export PATH="$PATH:/opt/shop/bin"' >> ~/.bashrcThe single quotes matter: they keep $PATH as text in the file, so it is expanded each time a shell starts. Load the change into your current shell with:
source ~/.bashrcMake shopctl work in every new shell for root by adding /opt/shop/bin to PATH in ~/.bashrc, keeping all the existing folders.
Expected result: a brand-new shell (bash) can run shopctl version, and ls still works in it.
Hint: the commands, explained
echo 'export PATH="$PATH:/opt/shop/bin"' >> ~/.bashrc
bash -c 'source ~/.bashrc; shopctl version'What each part does
echo '...' >> ~/.bashrc: append a line to your shell's startup file. The single quotes keep$PATHfrom being expanded now; it's expanded each time a shell starts.PATH="$PATH:/opt/shop/bin": keep the existing folders and add one at the end, so commands in/opt/shop/bincan be run by name.bash -c 'source ~/.bashrc; shopctl version': start a new shell that loads the file and runsshopctl, proving it's found.
Permanent settings and aliases
Add a personal alias for root and a variable that every user gets at login.
Why we do this: you will set up shells on servers, in Docker images and on CI runners. Knowing which file is read when is the difference between a setting that "works on my machine" and one that works for every user, every time.
1. Which files does bash read?
| File | Read by | Use it for |
|---|---|---|
/etc/profile and /etc/profile.d/*.sh | login shells, all users | system-wide variables |
~/.bash_profile or ~/.profile | login shells, one user | that user's login settings |
~/.bashrc | interactive shells, one user | aliases, prompt, functions, PATH |
/etc/bash.bashrc | interactive shells, all users | shared interactive settings |
A login shell is the first shell after you log in (SSH, su -). Every terminal tab or bash you start after that is an interactive shell. On Ubuntu, ~/.profile also reads ~/.bashrc, so in practice .bashrc is where most personal settings go.
ls /etc/profile.d/
head -30 ~/.bashrc2. Aliases: short names for long commands
alias
alias shoplogs='tail -n 20 /var/log/shop-app.log'
shoplogs
type shoplogsLike variables, an alias typed in the terminal disappears when the shell closes. Add it to ~/.bashrc to keep it. unalias shoplogs removes it.
3. A variable for every user
Files in /etc/profile.d/ are read by every user's login shell, which makes them the right place for settings everyone needs:
echo 'export SHOP_HOME=/opt/shop' > /etc/profile.d/shop.sh
su - deploy -c 'echo $SHOP_HOME'su - deploy starts a login shell as the deploy user, so it reads /etc/profile.d/. Without the -, it would keep root's environment instead, a common source of confusion.
- Add a permanent alias
shoplogsfor root, in~/.bashrc, that runstail -n 20 /var/log/shop-app.log. - Make every user get
SHOP_HOME=/opt/shopat login, using a file in/etc/profile.d/.
Expected result: a new root shell knows shoplogs, and su - deploy -c 'echo $SHOP_HOME' prints /opt/shop.
Hint: the commands, explained
echo "alias shoplogs='tail -n 20 /var/log/shop-app.log'" >> ~/.bashrc
echo 'export SHOP_HOME=/opt/shop' > /etc/profile.d/shop.shWhat each part does
alias shoplogs='...': a shortcut name for a longer command. In~/.bashrcit exists in every new shell for this user./etc/profile.d/shop.sh: scripts in this folder run for every user at login, soSHOP_HOMEbecomes available system-wide.
Configuration with a .env file
Move the app's settings and password into a protected .env file that Git ignores.
Why we do this: passwords hard-coded in scripts end up in Git history, in screenshots and in backups. The standard approach is a .env file that only the app's owner can read and Git never sees. Docker (--env-file), Docker Compose and most frameworks read the same format.
1. The problem
cat /opt/shop/deploy-old.shThe database password is right there, readable by anyone who can read the file, and one git add . away from being public forever.
2. The .env format
One NAME=value per line, no export, no spaces around =:
APP_ENV=production
DB_HOST=db.internal
DB_PASSWORD=S3cret-Shop-2026Create it:
nano /opt/shop/.env3. Lock it down
chmod 600 /opt/shop/.env
ls -l /opt/shop/.env600 means only the owner can read or write it.
4. Load it
Look at how the launcher reads it:
cat /opt/shop/start.shset -a makes every variable defined afterwards exported automatically, then . /opt/shop/.env (the same as source) reads the file, and set +a switches that off again. Start the app:
/opt/shop/start.sh5. Keep it out of Git
cd /opt/shop
git status
echo ".env" >> .gitignore
git statusAfter adding it to .gitignore, .env no longer appears as a file waiting to be committed. A committed secret stays in the history even after you delete it, so in real life it would also have to be changed.
In Kubernetes the same values would come from a ConfigMap (settings) and a Secret (passwords). In AWS, from Parameter Store or Secrets Manager.
- Create
/opt/shop/.envwithAPP_ENV=production,DB_HOST=db.internaland aDB_PASSWORD, readable only by its owner (mode600). - Add
.envto/opt/shop/.gitignore. - Start the app with
/opt/shop/start.sh.
Expected result: the last line of /var/log/shop-app.log shows launcher=start.sh APP_ENV=production DB_HOST=db.internal DB_PASSWORD=(set), and git status doesn't list .env.
Hint: the commands, explained
printf 'APP_ENV=production\nDB_HOST=db.internal\nDB_PASSWORD=S3cret-Shop-2026\n' > /opt/shop/.env
chmod 600 /opt/shop/.env
echo ".env" >> /opt/shop/.gitignore
/opt/shop/start.shWhat each part does
printf 'A\nB\nC\n' > FILE: write three lines (\n= new line) into the.envfile.chmod 600: only the owner can read or write it, because it contains a password.echo ".env" >> .gitignore: tell Git never to commit this file.start.shreads the variables from.envat startup.
Brilliant work!
You configured the shop the way real deployments do: from the environment, with a working PATH, permanent shell settings and secrets kept in a protected, Git-ignored .env file.
Interview-ready takeaways:
NAME=valueis a shell variable;export NAME=valuepasses it to child processes;NAME=value cmdsets it for one command. Children can't change their parent's environment.PATHis searched left to right. Extend it withPATH="$PATH:/dir", never replace it.typeandwhichshow where a command comes from.- Login shells read
/etc/profile,/etc/profile.d/and~/.profile; interactive shells read~/.bashrc.su -gives a login shell,sourcereloads a file into the current shell. - Secrets go in a
chmod 600.env(or a secret manager), loaded withset -a; . .env; set +aordocker run --env-file, and listed in.gitignore.
Next: the SSH Keys & Remote Access lab.
Understand shell and environment variables, fix "command not found" with PATH, make settings permanent in .bashrc and /etc/profile.d, and configure an app with a .env file.
The shop's app reads its settings from environment variables, but it keeps starting in the wrong mode, its admin tool says "command not found", and the database password is sitting in a script. You will configure the app the way it is done in real deployments, from the shell to a protected .env file.