Course contentsShow
← Linux
Lesson 25 of 1,8191. Shell Architecture and Process EnvironmentFree lesson

Variable Expansion and Tokenization

Manipulate shell variables during command interpretation to avoid common evaluation vulnerabilities.

In this lesson, you will learn how the shell transforms a command line string into a set of arguments by expanding variables before passing them to the operating system.

The Shell’s "Preprocessing" Step

Before a shell executes a command, it performs a series of transformations on the text you type. Two of the most critical steps in this process are Variable Expansion and Tokenization.

When you type a command like echo $USER, the shell does not pass the literal string "$USER" to the execve system call. Instead, it looks up the value of the environment variable USER and replaces the token with the actual data (e.g., "alice"). This happens inside the shell process before it forks a child.

Why Tokenization Matters

Tokenization is the process of breaking your command line into distinct "words" or arguments based on spaces, tabs, or newlines. Problems arise when expansion and tokenization interact. Consider this:

FILE_NAME="my notes.txt"
ls $FILE_NAME

If you run this, the shell expands $FILE_NAME into my notes.txt. Because there is a space in the variable, the shell’s tokenizer sees two separate tokens: my and notes.txt. The ls command then attempts to list two different files instead of one, leading to an "Environment Vulnerability"—where your command behaves unexpectedly because it interpreted your data as structural code.

Avoiding Evaluation Vulnerabilities

To ensure your variables are treated as a single unit, you must use quoting. By wrapping the variable in double quotes ("$FILE_NAME"), you tell the shell: "Perform the expansion, but do not re-tokenize the result." The shell maintains the space as part of the filename token, ensuring the execve call receives the exact argument you intended.

When designing scripts, always assume that variables might contain spaces or special characters. By quoting your expansions, you prevent the shell from accidentally "breaking" your data into pieces the command wasn't meant to handle.

Key Takeaway: Always wrap your variable expansions in double quotes to prevent the shell from re-tokenizing the resulting value and causing unintended command behavior.