BASH / CHAPTER 13

Testing exact arguments and failure behavior

Advanced

Verify both output bytes and command status at boundary cases.

By the end of this chapter

  • Test empty and whitespace-containing inputs
  • Check status separately from output
  • Use deterministic command substitutes

The idea

A command-line tool has several observable outputs: stdout, stderr, exit status, and any intended changes to files. Testing only visible text misses failures where a script prints the right message but reports success incorrectly. State the contract for each channel, then check them separately. For functions that build commands, a test substitute can record argument counts and values to reveal accidental splitting.

Boundary inputs should include an empty argument, spaces, wildcard characters, a leading hyphen, and no arguments when supported. These expose assumptions hidden by simple filenames. Avoid tests that depend on the contents of an uncontrolled directory or the timing of a live service. Use a small isolated fixture and explicit synchronization, and compare exact results rather than merely checking whether some nonempty output appeared.

Bash example
show_args(){ printf '%s\n' "$#"; printf '<%s>\n' "$@"; }
show_args "two words" "" "*.txt"
Result
3
<two words>
<>
<*.txt>

The function receives exactly three arguments, including an empty one and a literal pattern string.

Try it yourself

Test a wrapper with an empty operand, a spaced operand, and a literal wildcard, checking argument count and values.

Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.

CHECK YOUR UNDERSTANDING

Chapter checkpoint

Which input best exposes accidental word splitting?