api: YAML JSON TOON Database
version: 1.0.0
format: json
dataset:
  id: 1014
  slug: shell-safe-file-patterns
  title: Safer Bash File and Process Patterns
  description: Bash patterns for filenames, temporary files, pipelines, and exit status, with portability boundaries.
  summary: 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_when:
    - Write a script that handles unusual filenames.
    - Keep a maintenance command bounded and recoverable.
  caveats:
    - The examples target Bash with common GNU tools.
    - Cleanup commands must only target directories your task owns.
  related:
    - shell-scripting-guide
    - git-recovery-workflows
  category: Unix Commands
  category_slug: unix-commands
  tags: unix-commands,shell safe file patterns,reference,engineering
  view_count: 0
  created_at: 1791798640
  updated_at: 1791798640
  reviewed_at: "2026-10-12"
  sources:
    - title: GNU Bash manual
      url: "https://www.gnu.org/software/bash/manual/"
    - title: GNU Coreutils manual
      url: "https://www.gnu.org/software/coreutils/manual/coreutils.html"
  reading:
    - recover-git-work-without-losing-local-changes
  provenance: Original editorial reference notes by YJTOON, informed by the linked primary documentation. External specifications retain their own licenses.
  entry_count: 10
  fields:
    - section: data
      field: task
      type: string
      example: Quote a path
    - section: data
      field: pattern
      type: string
      example: "printf '%s\\n' \"$file\""
    - section: data
      field: reason
      type: string
      example: Keep spaces and glob characters in one argument.
    - section: data
      field: limitation
      type: string
      example: Quoting does not validate the value.
data:
  - task: Quote a path
    pattern: "printf '%s\\n' \"$file\""
    reason: Keep spaces and glob characters in one argument.
    limitation: Quoting does not validate the value.
  - task: Stop option parsing
    pattern: "cp -- \"$source\" \"$destination\""
    reason: Treat a leading hyphen as part of a filename.
    limitation: Double-dash support depends on the utility.
  - task: Read a literal line
    pattern: IFS= read -r line
    reason: Preserve backslashes and leading spaces.
    limitation: A line format cannot represent embedded newlines safely.
  - task: Print data
    pattern: "printf '%s\\n' \"$value\""
    reason: Keep user-controlled text out of the format string.
    limitation: Never use the value itself as the printf format.
  - task: Create a temporary directory
    pattern: workdir=$(mktemp -d)
    reason: Avoid predictable temporary names.
    limitation: Arrange cleanup and check failure.
  - task: Clean up on exit
    pattern: "trap 'rm -rf -- \"$workdir\"' EXIT"
    reason: Remove a task-owned temporary directory.
    limitation: Validate that the variable refers to the intended directory.
  - task: Propagate pipeline failure
    pattern: set -o pipefail
    reason: Let a failing earlier pipeline stage affect status.
    limitation: This is not a POSIX sh feature.
  - task: Use an argument array
    pattern: "args=(--output \"$file\"); command \"${args[@]}\""
    reason: Preserve argument boundaries.
    limitation: Bash arrays are not portable to plain sh.
  - task: Use NUL-delimited paths
    pattern: find . -type f -print0
    reason: Represent paths that contain whitespace or newlines.
    limitation: Every consumer must preserve NUL delimiters.
  - task: Bound a process
    pattern: timeout 10s command
    reason: Limit a potentially stuck command.
    limitation: GNU timeout is not available on every platform.
