# Created on savesnippets.com ยท https://savesnippets.com/1kzibYXSvfbk1S atomic_write() { local target="$1" content="$2" local dir dir="$(dirname "$target")" local tmp tmp="$(mktemp "$dir/.$(basename "$target").XXXXXX")" printf '%s' "$content" > "$tmp" mv "$tmp" "$target" # atomic on POSIX, same filesystem } # Usage atomic_write /var/lib/myapp/state.json '{"ready": true}' # Concurrent-safe pattern for cron jobs writing the same file: update_status() { local out="/var/lib/myapp/status.txt" atomic_write "$out" "Last run: $(date -u +%FT%TZ)" }