2.1a Bash Shell, Command Syntax & Variables
Key Takeaways
- Bash is the default interactive shell on most Linux distributions and reads each line as a command name followed by options and arguments
- echo prints text or expanded variable values; unquoted $NAME expands a variable, while echo alone with no arguments prints a blank line
- PATH is a colon-separated list of directories the shell searches for executable commands when you type a bare command name
- export promotes a shell variable into an environment variable so child processes inherit it; without export, children do not see the value
- type reports whether a name is a builtin, an alias, a function, a hashed binary, or not found — useful before you assume a command is an external program
The Linux Essentials exam expects you to work comfortably at a text prompt. On nearly every modern distribution that prompt is provided by Bash (the Bourne Again SHell). Bash reads a line, splits it into words, expands special tokens, then runs the resulting command. Mastering that pipeline — syntax, variables, and how the shell finds programs — is the core of objective 2.1.
What the Shell Actually Does
When you open a terminal, Bash prints a prompt (often ending in $ for a normal user or # for root) and waits. You type a command and press Enter. Conceptually Bash then:
- Tokenizes the line into words separated by spaces or tabs.
- Expands variables (
$HOME), wildcards, and command substitutions where quoting allows it. - Resolves the first word as the command name (builtin, alias, function, or external file).
- Passes the remaining words as options and arguments.
- Waits for the command to finish (unless you background it — beyond Essentials depth).
A typical line looks like:
ls -la /etc
| Piece | Role | Example |
|---|---|---|
| Command | Program or builtin to run | ls |
| Options / switches | Modify behavior; often start with - or -- | -la |
| Arguments | Targets such as files or directories | /etc |
Order matters for readability and for many tools: command first, then options, then operands. Some utilities accept options after arguments, but the exam-safe habit is options before operands.
echo: Print What the Shell Sees
echo writes its arguments to standard output, separated by spaces, and ends with a newline:
echo Hello Linux
# Hello Linux
echo "HOME is $HOME"
# HOME is /home/student
Because echo prints after expansion, it is the simplest way to inspect what Bash did with a variable or a quoted string. Options you may see:
| Option | Effect |
|---|---|
-n | Suppress the trailing newline |
-e | Enable backslash escapes such as \n and \t (implementation-dependent; know that behavior varies) |
For Essentials, focus on using echo to display literal text and expanded $variables.
Variables: Shell vs Environment
A shell variable exists only inside the current Bash process. Assign with NAME=value — no spaces around =:
COURSE=LinuxEssentials
echo $COURSE
# LinuxEssentials
An environment variable is a shell variable that has been marked for export. Child processes (commands you launch) inherit the environment. Shell-only variables do not appear in children.
SECRET=local-only
export PUBLIC=visible-to-children
bash -c 'echo SECRET=$SECRET PUBLIC=$PUBLIC'
# SECRET= PUBLIC=visible-to-children
| Concept | How you set it | Visible to child processes? |
|---|---|---|
| Shell variable | NAME=value | No |
| Environment variable | export NAME=value or NAME=value then export NAME | Yes |
Common environment variables you should recognize:
| Variable | Typical meaning |
|---|---|
HOME | Your home directory |
USER / LOGNAME | Login name |
SHELL | Path to your login shell |
PATH | Directories searched for commands |
PWD | Current working directory |
PATH: How Bash Finds Commands
When you type ls without a slash, Bash does not magically know /bin/ls. It walks the directories listed in PATH, separated by colons:
echo $PATH
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
The first matching executable wins. If nothing matches, Bash reports command not found. Putting . (the current directory) in PATH is discouraged for security; run local scripts with ./script.sh instead.
To temporarily prepend a directory:
export PATH="$HOME/bin:$PATH"
Children of this shell then search $HOME/bin first.
export and type
export either marks an existing variable for the environment or assigns and exports in one step:
export EDITOR=nano
export PATH="/opt/tools/bin:$PATH"
type tells you how Bash would run a name — critical when aliases or builtins shadow external binaries:
type echo
# echo is a shell builtin
type ls
# ls is /bin/ls (or "ls is aliased to ...")
type notacommand
# bash: type: notacommand: not found
type result | Meaning |
|---|---|
| shell builtin | Implemented inside Bash (cd, echo, export, type itself) |
| aliased to … | Shortcut defined with alias |
| hashed / is /path | External executable on disk |
| not found | Nothing on PATH (and not a builtin/alias) |
Command Substitution Awareness
Command substitution runs a command and inserts its output into the outer command line. Modern form uses $(...):
echo "Today is $(date +%Y-%m-%d)"
echo "Kernel: $(uname -r)"
Older textbooks show backticks `date`; prefer $(...) because it nests cleanly. For Essentials, know that substitution happens before the outer command runs, and that quoting around $(...) preserves spaces in the captured output.
Worked Mini-Lab
# 1. Create a shell-only variable
LAB=essentials
echo "LAB=$LAB"
# 2. Export so a child shell sees it
export LAB
bash -c 'echo child sees LAB=$LAB'
# 3. Inspect how commands resolve
type pwd
type /bin/pwd
# 4. Confirm PATH search order
echo $PATH
which ls # often shows the same path type would report for an external binary
If bash -c prints an empty LAB, you forgot export. That single distinction — shell versus environment — is a frequent exam trap.
Exam Focus
Expect fill-in and multiple-choice items on: correct NAME=value syntax (no spaces), what export changes, reading PATH, and using echo/type to inspect the environment. You do not need deep scripting yet — that arrives in Topic 3 — but you must treat the shell as a programmable environment, not just a place to type ls.
A user runs COURSE=linux and then starts a new Bash process with bash -c 'echo $COURSE'. The child prints a blank line. What is the most likely reason?
What does the PATH environment variable contain?
You run type echo and the shell replies that echo is a shell builtin. What does that mean for command execution?