Safer Bash File and Process Patterns
Bash patterns for filenames, temporary files, pipelines, and exit status, with portability boundaries.
Bash patterns for filenames, temporary files, pipelines, and exit status, with portability boundaries.
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.
| Task | Pattern | Reason | Limitation |
|---|---|---|---|
| Quote a path | printf '%s\n' "$file" | Keep spaces and glob characters in one argument. | Quoting does not validate the value. |
| Stop option parsing | cp -- "$source" "$destination" | Treat a leading hyphen as part of a filename. | Double-dash support depends on the utility. |
| Read a literal line | IFS= read -r line | Preserve backslashes and leading spaces. | A line format cannot represent embedded newlines safely. |
| Print data | printf '%s\n' "$value" | Keep user-controlled text out of the format string. | Never use the value itself as the printf format. |
| Create a temporary directory | workdir=$(mktemp -d) | Avoid predictable temporary names. | Arrange cleanup and check failure. |
| Clean up on exit | trap 'rm -rf -- "$workdir"' EXIT | Remove a task-owned temporary directory. | Validate that the variable refers to the intended directory. |
| Propagate pipeline failure | set -o pipefail | Let a failing earlier pipeline stage affect status. | This is not a POSIX sh feature. |
| Use an argument array | args=(--output "$file"); command "${args[@]}" | Preserve argument boundaries. | Bash arrays are not portable to plain sh. |
| Use NUL-delimited paths | find . -type f -print0 | Represent paths that contain whitespace or newlines. | Every consumer must preserve NUL delimiters. |
| Bound a process | timeout 10s command | Limit a potentially stuck command. | GNU timeout is not available on every platform. |
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.
| Section | Field | Type | Example |
|---|---|---|---|
| data | task | string | Quote a path |
| data | pattern | string | printf '%s\n' "$file" |
| data | reason | string | Keep spaces and glob characters in one argument. |
| data | limitation | string | Quoting does not validate the value. |
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 →
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.