A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Command-Line Arguments and Environment Variables

College Depth 95 in the knowledge graph ☐ I know this ☆ Set as goal
357prerequisites beneath it
See this on the map →
Process Environment and Exit Codes→
process arguments environment

Core Idea

Processes receive command-line arguments through argc/argv and environment variables through an environment array, providing the primary means of configuring process behavior at startup. The shell constructs these when executing a command, expanding wildcards and substituting variable values. Both are inherited by child processes unless explicitly modified.

Explainer

From your understanding of process environments and exit codes, you know that every process carries an environment — a collection of state that defines its execution context. Command-line arguments and environment variables are two of the most important parts of that environment, and they serve complementary purposes: arguments tell a process *what to do right now*, while environment variables tell it *what context it is running in*.

When you type `ls -l /home/user` in a shell, the shell creates a new process and passes three strings as command-line arguments: `"ls"`, `"-l"`, and `"/home/user"`. In C, the process receives these through the `main` function's parameters: `argc` (the count, here 3) and `argv` (an array of string pointers). The first element, `argv[0]`, is conventionally the program's own name. This mechanism is simple and direct — the arguments exist only for this invocation and are not inherited by any processes that `ls` might spawn. They are the equivalent of function parameters in a programming language.

Environment variables work differently. They are key-value pairs (like `PATH=/usr/bin:/bin` or `HOME=/home/user`) stored in a block of memory that the process can access through the `environ` global variable or the `getenv()` function. Unlike arguments, environment variables are inherited by child processes through `fork()` and `exec()`. When the shell launches `ls`, `ls` inherits the shell's entire environment — `PATH`, `HOME`, `LANG`, `TERM`, and dozens of others. This inheritance chain is how configuration propagates through a process hierarchy without every program needing to know about every setting. A program that needs to know the user's preferred language checks `LANG`; a program that needs to find executables checks `PATH`.

The shell plays a crucial role in constructing both mechanisms. Before passing arguments to a new process, the shell performs expansions: wildcards like `*.txt` are expanded into matching filenames, variables like `$HOME` are replaced with their values, and quotes control which expansions apply. The command `echo $HOME/*.txt` might reach the process as `echo /home/user/notes.txt /home/user/todo.txt` — the process never sees the wildcard or the variable reference. Understanding that these transformations happen *in the shell before the process starts* is essential to predicting what a program actually receives.

A process can modify its own environment (using `setenv()` or `putenv()`) and these changes are inherited by any children it subsequently creates, but they never propagate *upward* to the parent. This is why running `export PATH=/new/path` in a script does not change the shell that launched the script — the script runs in a child process whose environment changes die with it. To affect the parent shell's environment, you must *source* the script (`. script.sh` or `source script.sh`), which executes the commands in the current shell process rather than a child.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Environment and Exit Codes → Command-Line Arguments and Environment Variables

Longest path: 96 steps · 357 total prerequisite topics

Prerequisites (1)

Leads To (0)

No topics depend on this one yet.