Skip to content
AI Engineer Guide.

concept · basics · intermediate · concept 20 of 24

Linux & shell

0 of 24 complete

Why it matters

Almost every deployment, scheduled job and debugging session runs through a shell, and the shell's defaults are permissive in ways that hide failure: an unset variable expands to empty, a failed command in a pipeline is ignored, and a glob with no match passes through literally. Scripts that work interactively fail in automation because the interactive shell supplied context the script does not have.

Mental model

It is a set of pipes connecting tools: each tool does one job and passes text to the next. The analogy breaks because the pipe carries bytes without a schema, so each stage guesses the format and failures travel as text rather than as errors.

How it works

Mechanism, step by stepInteractive
  1. 1Start scripts with strict mode, and handle expected failures explicitly rather than letting the first error abort silently.
  2. 2Quote every expansion and use null-delimited iteration for filenames, so spaces and newlines cannot change the command.
  3. 3Check exit codes at each boundary, and log the failing command with its arguments.
  4. 4Use traps to clean up temporary files and child processes on exit and on termination signals.
  5. 5Prefer a structured tool for JSON or CSV rather than splitting on whitespace with text utilities.
  6. 6Test the script with empty input, a missing file and a path containing a space.

Step 1 of 6

Start scripts with strict mode, and handle expected failures explicitly rather than letting the first error abort silently.

Step 1/6 · step 1

Figure. The mechanism in this page's own steps. The figure adds sequence, not new claims.

What to notice. A step can be read alone, but the order is the mechanism. The control lets you stop on any one.

Text version

The page's steps: 1. Start scripts with strict mode, and handle expected failures explicitly rather than letti… 2. Quote every expansion and use null-delimited iteration for filenames, so spaces and newli… 3. Check exit codes at each boundary, and log the failing command with its arguments. 4. Use traps to clean up temporary files and child processes on exit and on termination sign… 5. Prefer a structured tool for JSON or CSV rather than splitting on whitespace with text ut… 6. Test the script with empty input, a missing file and a path containing a space.

  1. Start scripts with strict mode, and handle expected failures explicitly rather than letting the first error abort silently.
  2. Quote every expansion and use null-delimited iteration for filenames, so spaces and newlines cannot change the command.
  3. Check exit codes at each boundary, and log the failing command with its arguments.
  4. Use traps to clean up temporary files and child processes on exit and on termination signals.
  5. Prefer a structured tool for JSON or CSV rather than splitting on whitespace with text utilities.
  6. Test the script with empty input, a missing file and a path containing a space.

Trade-offs

ChoiceGood forCostFails when
Strict modeFails fast and loudlyUnset optional variables abort the scriptThe script reads optional environment configuration
One long pipelineConcise and fastHard to debug and no intermediate checksAny stage can fail for different reasons
Shell scriptNo dependencies, direct access to system toolsWeak typing and error handlingThe logic has branches and state
Compiled or scripted toolStructure and testabilityRuntime dependency and startup costThe task is a three-line orchestration
Trade-off mapInteractive

Good for

Fails fast and loudly

Cost

Unset optional variables abort the script

Fails when

The script reads optional environment configuration

  1. The script reads optional environment configuration
  2. Any stage can fail for different reasons
  3. The logic has branches and state
  4. The task is a three-line orchestration

Figure. Each choice with what it buys, what it costs, and the case where it loses.

What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.

Text version

Four trade-offs: Strict mode is good for Fails fast and loudly, costs Unset optional variables abort the script, and fails when The script reads optional environment configuration. One long pipeline is good for Concise and fast, costs Hard to debug and no intermediate checks, and fails when Any stage can fail for different reasons. Shell script is good for No dependencies, direct access to system tools, costs Weak typing and error handling, and fails when The logic has branches and state. Compiled or scripted tool is good for Structure and testability, costs Runtime dependency and startup cost, and fails when The task is a three-line orchestration

Failure modes

  • Pipeline masks failure. A middle command fails and the script continues with empty input. Debug: enable pipefail and rerun; the exit status will point at the failing stage.
  • Word splitting. A filename with a space is treated as two arguments and the wrong file is deleted. Debug: rerun with the filename quoted in a test fixture and inspect the argument list.
  • Orphaned child process. The script exits and leaves a background job holding a port. Debug: list the process group after exit and add a trap that terminates children.

In production

Log the command, exit code and duration of every automated shell step, and alert on non-zero exits that were previously ignored. Keep scripts in version control with a static analysis step, since quoting bugs are mechanical to find.

Common misconceptions

People think: The shell is not a real programming language, so it does not need tests.

Actually: Automation runs unattended, which makes quoting and exit-code bugs more likely to matter, not less.

People think: Strict mode makes a script safe.

Actually: It has exceptions and does not apply inside pipelines or conditionals, so explicit checks are still required.

Interview angle

Treat exit codes and quoting as the contract, and mention traps and structured parsing. Know what strict mode does and does not cover. A pipeline with no account of how failure is detected is the weak answer.

Sources