Environment Variables & Shell Config Beginner 30 min
Lab overview

Environment Variables & Shell Config

30 min 4 checked steps Beginner
The situation

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.

By the end of this lab you will be able to
  • 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.

KindCreated withSeen by programs you start?
Shell variableNAME=valueNo, only by this shell
Environment variableexport NAME=valueYes, copied to every child process
For one commandNAME=value commandOnly 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).

The steps
1
Variables and exportStart the shop app in production mode with the right database host.
2
PATH and "command not found"Make the shopctl admin tool work in every new shell, without breaking other commands.
3
Permanent settings and aliasesAdd a personal alias for root and a variable that every user gets at login.
4
Configuration with a .env fileMove the app's settings and password into a protected .env file that Git ignores.
How this lab works
  1. Press Start lab. You get your own Ubuntu machine on the right, just for you.
  2. Read each step. Commands with a ▶ Run button can be sent to the terminal in one click, but typing them yourself helps you remember.
  3. 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.
  4. Stuck? Open the hint. Nothing you do here can break anything real.
Step 1 of 4

Variables and export

Objective

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

bash
env | sort | head -20
echo "$HOME $USER $SHELL"
printenv PATH

env and printenv list the exported variables. echo $NAME prints one value.

2. A shell variable is not enough

bash
cat /opt/shop/app.sh
APP_ENV=staging
echo "$APP_ENV"
/opt/shop/app.sh

Your 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

bash
export APP_ENV=staging
/opt/shop/app.sh

Now the app sees it. Check with env | grep APP_.

4. For one command only

bash
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

bash
unset APP_ENV
/opt/shop/app.sh

Every 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
text
export APP_ENV=production DB_HOST=db.internal
/opt/shop/app.sh

What 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_ENV and DB_HOST from its environment and starts with them.
Step 2 of 4

PATH and "command not found"

Objective

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

bash
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.

bash
which ls
type ls cd ll

which 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

bash
shopctl status
ls -l /opt/shop/bin/
/opt/shop/bin/shopctl status

The tool exists and works with its full path, but /opt/shop/bin is not in PATH.

3. Add a folder to PATH

bash
export PATH="$PATH:/opt/shop/bin"
shopctl status

Always 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:

bash
echo 'export PATH="$PATH:/opt/shop/bin"' >> ~/.bashrc

The 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:

bash
source ~/.bashrc

Make 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
text
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 $PATH from 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/bin can be run by name.
  • bash -c 'source ~/.bashrc; shopctl version': start a new shell that loads the file and runs shopctl, proving it's found.
Step 3 of 4

Permanent settings and aliases

Objective

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?

FileRead byUse it for
/etc/profile and /etc/profile.d/*.shlogin shells, all userssystem-wide variables
~/.bash_profile or ~/.profilelogin shells, one userthat user's login settings
~/.bashrcinteractive shells, one useraliases, prompt, functions, PATH
/etc/bash.bashrcinteractive shells, all usersshared 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.

bash
ls /etc/profile.d/
head -30 ~/.bashrc

2. Aliases: short names for long commands

bash
alias
alias shoplogs='tail -n 20 /var/log/shop-app.log'
shoplogs
type shoplogs

Like 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:

bash
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.

  1. Add a permanent alias shoplogs for root, in ~/.bashrc, that runs tail -n 20 /var/log/shop-app.log.
  2. Make every user get SHOP_HOME=/opt/shop at 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
text
echo "alias shoplogs='tail -n 20 /var/log/shop-app.log'" >> ~/.bashrc
echo 'export SHOP_HOME=/opt/shop' > /etc/profile.d/shop.sh

What each part does

  • alias shoplogs='...': a shortcut name for a longer command. In ~/.bashrc it exists in every new shell for this user.
  • /etc/profile.d/shop.sh: scripts in this folder run for every user at login, so SHOP_HOME becomes available system-wide.
Step 4 of 4

Configuration with a .env file

Objective

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

bash
cat /opt/shop/deploy-old.sh

The 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 =:

text
APP_ENV=production
DB_HOST=db.internal
DB_PASSWORD=S3cret-Shop-2026

Create it:

bash
nano /opt/shop/.env

3. Lock it down

bash
chmod 600 /opt/shop/.env
ls -l /opt/shop/.env

600 means only the owner can read or write it.

4. Load it

Look at how the launcher reads it:

bash
cat /opt/shop/start.sh

set -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:

bash
/opt/shop/start.sh

5. Keep it out of Git

bash
cd /opt/shop
git status
echo ".env" >> .gitignore
git status

After 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.

  1. Create /opt/shop/.env with APP_ENV=production, DB_HOST=db.internal and a DB_PASSWORD, readable only by its owner (mode 600).
  2. Add .env to /opt/shop/.gitignore.
  3. 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
text
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.sh

What each part does

  • printf 'A\nB\nC\n' > FILE: write three lines (\n = new line) into the .env file.
  • 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.sh reads the variables from .env at startup.
Lab complete

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=value is a shell variable; export NAME=value passes it to child processes; NAME=value cmd sets it for one command. Children can't change their parent's environment.
  • PATH is searched left to right. Extend it with PATH="$PATH:/dir", never replace it. type and which show where a command comes from.
  • Login shells read /etc/profile, /etc/profile.d/ and ~/.profile; interactive shells read ~/.bashrc. su - gives a login shell, source reloads a file into the current shell.
  • Secrets go in a chmod 600 .env (or a secret manager), loaded with set -a; . .env; set +a or docker run --env-file, and listed in .gitignore.

Next: the SSH Keys & Remote Access lab.

root@devopsshack offline

Your Linux machine is ready to launch

Starting gives you a private Ubuntu server for up to 45 minutes. It is deleted when you finish.

Feedback