🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Web & Software Development › Linux/Bash for Web & Software Development › Lesson 11
TEC-WEB46-06 · Lesson 11 · 14 clock hours · lab

Shell Environment, Variables, and Startup Files

Objective. Students will configure the shell environment by setting and exporting variables, modifying PATH, and customizing .bashrc and .bash_profile to support a reproducible development workspace.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Shell Variables vs. Environment Variables

When you open a Bash terminal, the shell maintains two overlapping collections of data: shell variables and environment variables. Understanding the difference is essential for configuring a reliable development workspace.

A shell variable exists only inside the current shell process. You create one by assigning a name and value:

```bash

MY_VAR="hello"

```

No other process can see `MY_VAR`. When you launch a script or a child shell, it does not inherit this variable.

An environment variable is a shell variable that has been exported. Exporting marks the variable so that every child process the shell spawns inherits a copy of it:

```bash

export MY_VAR="hello"

```

You can combine assignment and export on one line, which is the most common style.

To inspect environment variables, use `env` or `printenv`. To see all variables — including unexported shell variables and shell functions — use `set`. The difference matters in practice: a variable you forget to export will appear in `set` output but not in `env`, and a script run as a child process will not see it.

The PATH Variable

`PATH` is one of the most important environment variables. It is a colon-separated list of directories the shell searches, in order, when you type a command name. If a directory is not in `PATH`, you must type the full path to any executable stored there.

Developers commonly create a personal `~/bin` directory to store custom scripts and small tools. To make those files available as commands, `~/bin` must be added to `PATH`:

```bash

export PATH="$HOME/bin:$PATH"

```

Placing `$HOME/bin` at the front gives your personal scripts priority over system-wide commands with the same name. Be intentional about ordering: putting an untrusted directory at the front of `PATH` is a security risk if others can write to it. Keep `~/bin` writable only by you (`chmod 700 ~/bin` or `chmod 755 ~/bin` with appropriate permissions).

Startup Files: .bashrc and .bash_profile

Bash reads different files depending on how the shell was started.

Because these two startup paths exist, developers use a common pattern: put all customizations in `.bashrc` and source `.bashrc` from `.bash_profile`:

```bash

[[ -f ~/.bashrc ]] && source ~/.bashrc

```

This ensures your settings load regardless of how the shell starts.

Aliases defined in `.bashrc` are also limited to the current shell; they are not inherited by child processes. That is expected behavior — aliases are a convenience shortcut, not a subprocess tool.

Example alias for a common Git command:

```bash

alias gst="git status"

```

After editing `.bashrc`, apply changes to the running shell with:

```bash

source ~/.bashrc

```

or the equivalent shorthand `. ~/.bashrc`.

Setting a Project Variable

A `PROJECT_ROOT` environment variable gives scripts and tools a single, authoritative reference to your project directory. Set and export it in `.bashrc`:

```bash

export PROJECT_ROOT="$HOME/projects"

```

Scripts can then use `$PROJECT_ROOT` instead of hardcoded paths, making the workspace easier to move or reproduce.

Dotfile Portability and Setup Scripts

A dotfile is any configuration file whose name begins with a dot (`.bashrc`, `.bash_profile`, etc.). Keeping these files under version control and writing a `setup.sh` script to deploy them is a professional practice called dotfile management.

A minimal `setup.sh` might:

1. Copy your customized `.bashrc` to `~/`.

2. Copy your `.bash_profile` to `~/`.

3. Create `~/bin` if it does not exist.

4. Source `.bashrc` to apply changes immediately.

Test the script on a clean-user account to simulate a fresh environment before relying on it.

Safety and Best Practices

Key terms10

Shell variableA named value stored in the current shell process only; it is not passed to child processes or scripts.
Environment variableA shell variable that has been exported, making it available to the current shell and all child processes it spawns.
exportA Bash built-in command that marks a variable to be included in the environment of child processes.
PATHAn environment variable containing a colon-separated list of directories the shell searches to find executable commands.
.bashrcA Bash startup file read for interactive non-login shells; the standard location for aliases, functions, and PATH modifications.
.bash_profileA Bash startup file read for login shells; commonly used to source .bashrc so settings apply in all shell types.
sourceA Bash built-in (also written as a dot: .) that reads and executes commands from a file in the current shell, applying changes immediately without opening a child process.
AliasA user-defined shortcut that replaces a command string with another; defined with the alias built-in and available only in the current shell and its interactive descendants.
DotfileAny configuration file whose name begins with a dot, making it hidden from standard directory listings; examples include .bashrc and .bash_profile.
Login shellA shell session started at login (e.g., via SSH or a virtual console) that reads /etc/profile and then ~/.bash_profile for user configuration.
In class: Instructor demo: show the difference between shell variables and environment variables using 'env', 'set', and 'export'; demonstrate how a child process inherits only exported variables by running a sub-shell and echoing both types. · Workspace configuration lab: students add a custom ~/bin directory to PATH in .bashrc, create an alias for a frequently used git command, set a PROJECT_ROOT environment variable, and confirm changes persist across new terminal sessions. · Dotfile portability exercise: students write a setup.sh script that installs their .bashrc customizations on a fresh VM (simulated by a clean-user account), run it, and verify the environment is correctly reproduced.
Materials: Student VMs, Bash manual (startup files section), dotfile lab instruction sheet
Check: Instructor opens a new terminal on each student VM and verifies PATH contains ~/bin, the alias works, and PROJECT_ROOT is set — all three must be confirmed to pass.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

Linux fundamentals — shell environment
1. A developer sets `EDITOR=nano` in a Bash session without using `export`. A script launched from that shell tries to read `$EDITOR`. What will the script see?
Linux fundamentals — shell environment
2. Which command lets you view only the exported environment variables in the current shell session?
Linux fundamentals — shell and scripting
3. A student wants personal scripts in `~/bin` to be found before system commands of the same name. Which PATH assignment in `.bashrc` achieves this?
Linux fundamentals — startup files
4. After editing `.bashrc`, a student opens a new terminal tab and the changes are active, but when she logs in via SSH the changes are missing. What is the most likely cause?
Linux fundamentals — shell customization
5. What is the correct Bash syntax to define an alias named `gst` that runs `git status`?
Linux fundamentals — shell environment
6. A student's `setup.sh` script copies `.bashrc` to a new user account but the new user reports that the `PROJECT_ROOT` variable is not set after opening a terminal. Which line in `.bashrc` would correctly fix this?
Security — safe handling of credentials
7. Which of the following is the safest location to store an API key needed by a development script?
Linux fundamentals — shell scripting
8. A student runs `source ~/.bashrc` after adding a new alias. What does `source` do that running `bash ~/.bashrc` does not?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.