api: YAML JSON TOON Database version: 1.0.0 format: toon dataset: id: 1662 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[2]: Write a script that handles unusual filenames.,Keep a maintenance command bounded and recoverable. caveats[2]: The examples target Bash with common GNU tools.,Cleanup commands must only target directories your task owns. related[2]: shell-scripting-guide,git-recovery-workflows reviewed_at: 2026-10-12 sources[2]{title,url}: GNU Bash manual,"https://www.gnu.org/software/bash/manual/" GNU Coreutils manual,"https://www.gnu.org/software/coreutils/manual/coreutils.html" reading[1]: 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[4]{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. category: Unix Commands category_slug: unix-commands tags: "unix-commands,shell safe file patterns,reference,engineering" view_count: 2 created_at: 1791798640 updated_at: 1791798640 data[10]{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.