bash-scripting
БесплатноБез исполняемых скриптовНе проверенUse when writing or hardening a shell script that must survive another machine — a CI step, install script, cron job, git hook, devcontainer entrypoint…
от lza6★ 19 звёзд на GitHubОб этом скилле
Bash scripting — scripts that survive a stranger's machine
You are the shell author who has been burned: by an unquoted "$@" that
exploded a path with spaces, by rm -rf "$DIR/" where $DIR was empty, by a
trap that fired twice, by a set -e that swore it caught errors and didn't.
Write every script as if it will run in CI, in a container, and on a 2014 Mac
with bash 3.2 — because eventually it will.
The first decision is not a line of code. It is: which shell am I targeting? That choice decides what you are allowed to write. Make it before the shebang.
The header you start every bash script with
#!/usr/bin/env bash
set -euo pipefail # see "Strict mode, honestly" — this is a baseline, not a force field
IFS=$'\n\t' # split words on newline/tab only, never on spaces
#!/usr/bin/env bash— find bash onPATH; do not hardcode/bin/bash, which is 3.2 on macOS and may not exist on some images.set -e— exit on an uncaught non-zero command. Leaky (below), still worth having.set -u— error on an unset variable, so a typo'd$OUPUTfails loud instead of expanding to empty.set -o pipefail— a pipeline fails if any stage fails, not just the last. Bashism — not in POSIXsh.IFS=$'\n\t'— stops the classic "unquoted expansion splits on every space" bug at the source.
Pick your shell first
set -o pipefail, arrays, , and local are bashisms. If your shebang
is #!/bin/sh you may not get bash — on Debian/Alpine sh is dash/busybox.
Two traps to internalize: macOS /bin/bash is 3.2 (no declare -A,
no ${var,,}, no mapfile); and bash 5.3 added ${ cmd; } /
GLOBSORT / read -E that will not run on either of the above. Pick a floor
and stay above it — the full bash-vs-POSIX feature matrix, bash 3.2
workarounds, dash/busybox gotchas, the 5.3 features to guard, and how to test
one script under several shells are in
references/portability.md.
Strict mode, honestly
set -e is not a force field. It is silently suppressed in three places, and
people ship broken scripts because they trusted it:
- In an assignment with command substitution.
local x=$(failing)— the assignment succeeds (exit status is thelocal/assignment, not the substitution). - In a condition. Anything in
if,while,&&,||, or after!is exempt by design —set -ewould makeif grep …unusable otherwise. - Inside functions called in a condition: a failing line won't abort.
So: do not lean on set -e for control flow. Check what matters explicitly.
# Bad — set -e will NOT catch this; x is empty, script sails on
local x=$(curl -fsS "$url")
# Good — split declaration from assignment so the substitution's status is seen
local x
x=$(curl -fsS "$url") || { echo "fetch failed" >&2; return 1; }
trap 'echo "failed at line $LINENO" >&2' ERR gives you a breadcrumb on the
uncaught failures set -e does catch. To deliberately ignore a non-zero exit,
say so: cmd || true (and a comment why), never a bare unchecked cmd.
Quoting — the highest-value section
Unquoted expansions are the №1 cause of shell bugs and of the data-loss story everyone has heard. Quote every expansion unless you have a specific reason not to.
| Bad | Good | Why |
|---|---|---|
rm $file |
rm "$file" |
space/glob in $file becomes multiple args (SC2086) |
func $@ |
func "$@" |
"$@" preserves each arg verbatim; $@ re-splits them |
x=$(cmd) … echo $x |
echo "$x" |
unquoted output word-splits and glob-expands |
[ $x = y ] |
[ "$x" = y ] |
empty/spaced $x makes [ a syntax error |
for f in $(ls) |
for f in ./* |
parsing ls breaks on spaces/newlines (SC2045) |
rm -rf $DIR/ |
see below | the disaster |
The rm -rf disaster: if $DIR is unset/empty, rm -rf $DIR/ becomes
rm -rf /. Guard the variable and quote it:
: "${DIR:?DIR must be set}" # abort with a message if unset or empty
rm -rf "${DIR:?}"/ # belt and braces: fail rather than expand to /
Use … in bash (no word-splitting inside, supports =~, &&); use
[ … ] in POSIX sh. [ a == b ] is a bashism — POSIX [ uses =.
Arrays, not space-split strings
The instant an argument list is built from a string, spaces betray you. Build
it as an array and expand "${arr[@]}" (each element stays one argument).
# Bad — flags stri
Установить bash-scripting в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл bash-scripting?
Use when writing or hardening a shell script that must survive another machine — a CI step, install script, cron job, git hook, devcontainer entrypoint: strict-mode leaks, quoting/word-splitting, arrays, trap cleanup, bash-vs-POSIX portability, ShellCheck findings. NOT CI workflow structure, runners, caching or matrix (that is `github-actions`).
Как установить скилл bash-scripting?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл bash-scripting запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
docx
Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include…
от AnthropicUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple…
от Anthropicxlsx
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing…
от Anthropicpptx
Use this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or…
от Anthropic