Safer Bash File and Process Patterns

Bash patterns for filenames, temporary files, pipelines, and exit status, with portability boundaries.

  • JSON5,323 B
  • YAML3,768 B · −29%
  • TOON3,090 B · −42%

Bash patterns for filenames, temporary files, pipelines, and exit status, with portability boundaries. Each row includes a practical application and a failure to check before using it.

Use it when

  • Write a script that handles unusual filenames.
  • Keep a maintenance command bounded and recoverable.

Watch out for

  • The examples target Bash with common GNU tools.
  • Cleanup commands must only target directories your task owns.

The data

TaskPatternReasonLimitation
Quote a pathprintf '%s\n' "$file"Keep spaces and glob characters in one argument.Quoting does not validate the value.
Stop option parsingcp -- "$source" "$destination"Treat a leading hyphen as part of a filename.Double-dash support depends on the utility.
Read a literal lineIFS= read -r linePreserve backslashes and leading spaces.A line format cannot represent embedded newlines safely.
Print dataprintf '%s\n' "$value"Keep user-controlled text out of the format string.Never use the value itself as the printf format.
Create a temporary directoryworkdir=$(mktemp -d)Avoid predictable temporary names.Arrange cleanup and check failure.
Clean up on exittrap 'rm -rf -- "$workdir"' EXITRemove a task-owned temporary directory.Validate that the variable refers to the intended directory.
Propagate pipeline failureset -o pipefailLet a failing earlier pipeline stage affect status.This is not a POSIX sh feature.
Use an argument arrayargs=(--output "$file"); command "${args[@]}"Preserve argument boundaries.Bash arrays are not portable to plain sh.
Use NUL-delimited pathsfind . -type f -print0Represent paths that contain whitespace or newlines.Every consumer must preserve NUL delimiters.
Bound a processtimeout 10s commandLimit a potentially stuck command.GNU timeout is not available on every platform.

Fields

The shape of this dataset, read from the data itself: every column, its type and a real value. Agents can use this to plan a query without fetching the whole document.

SectionFieldTypeExample
datataskstringQuote a path
datapatternstringprintf '%s\n' "$file"
datareasonstringKeep spaces and glob characters in one argument.
datalimitationstringQuoting does not validate the value.

Fetch the same bytes

The static files contain the same reference rows as the API, with no API rate limit. Live metadata, such as view counts, may differ. Use the API for content negotiation; use the static files for a cacheable snapshot.

curl "https://yjtoon.com/api/dataset/shell-safe-file-patterns?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/shell-safe-file-patterns.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Sources and editorial notes

Original editorial reference notes by YJTOON, informed by the linked primary documentation. External specifications retain their own licenses.

Editorial review: . Source documents keep their own licenses.

Topics

  • unix-commands
  • shell safe file patterns
  • reference
  • engineering

Search the library

Search by a topic, title, or keyword.

Start with a collection

Browse all 205 datasets