Skip to content
Agentmart
Submit a tool

bash-script

Write bash scripts following best practices for safety, portability, and maintainability.

Skill by shmokmt

IndexedApache-2.0
Write a review

Chalk Labs

Need help deploying bash-script?

Chalk Labs sets up agents like this inside real teams: connected to your tools, tested on your data, with a human in the loop where it matters.

Book a call

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/shmokmt-bash-script

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

Write bash scripts following best practices for safety, portability, and maintainability. Use when creating new shell scripts or refactoring existing ones.

Use cases: writing

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md. Checked Oct 25, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/bash-script

What's inside

Skill name
bash-script
Description
Write bash scripts following best practices for safety, portability, and maintainability. Use when creating new shell scripts or refactoring existing ones.
License
Apache-2.0
Instructions
120 lines in SKILL.md · view on GitHub

Before installing

  • Instructions call the network

Files (1)

  • SKILL.md

SKILL.md (first lines)

# Bash Script

Write robust, portable, and maintainable bash scripts by applying a standard template, coding conventions, security rules, and portability guidelines.

## Steps

1. Clarify the script's purpose, target platform(s) (Linux-only, macOS-only, or both), and whether strict POSIX `sh` compatibility is required.
2. Copy the **Script Template** below and remove sections that are not needed (e.g., drop `getopts` if the script takes no arguments). Keep it minimal.
3. Implement the logic following the **Coding Conventions** and **Security Rules** sections.
4. If the script must run on both macOS and Linux, review the **Portability** section and apply the relevant workarounds.
5. Run `shellcheck <script>` (if available) and fix every warning. Explain each fix to the user.
6. Add a brief comment block at the top of the script (after the shebang) describing what it does.

## Script Template

```bash
#!/usr/bin/env bash
set -euo pipefail

readonly SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

cleanup() {
  # Remove temp files, restore state, etc.
  :
}
trap cleanup EXIT

usage() {
  cat <<EOF
Usage: ${SCRIPT_NAME} [options] <args>

Description of what this script does.

Options:
  -h    Show this help message
  -v    Enable verbose output
EOF
}

log() {
  printf '%s: %s\n' "${SCRIPT_NAME}" "$*" >&2
}

die() {
  log "error: $*"
  exit 1
}

main() {
  local verbose=0

  while getopts ":hv" opt; do
    case "${opt}" in
      h) usage; exit 0 ;;
      v) verbose=1 ;;
      :) die "option -${OPTARG} requires an argument" ;;
      *) die "unknown option -${OPTARG}" ;;
    esac
  done
  shift $((OPTIND - 1))

  # --- script logic starts here ---
}

main "$@"
```

## Coding Conventions

- **Variable names**: `snake_case` for locals, `UPPER_SNAKE_CASE` for constants and environment variables.
- **Quoting**: Always double-quote variable expansions and command substitutions — `"$var"`, `"$@"`, `"$(cmd)"`. Unquoted expansions are acceptable only inside `[[ ]]` on the right side of `=~` or `==` with intentional globbing.
- **Function definitions**: Use `fname() { }` form. Do not use the `function` keyword.
- **Conditionals**: Prefer `[[ ]]` over `[ ]`. Use `(( ))` for arithmetic.
- **Command substitution**: Use `$(command)`, never backticks.
- **Local variables**: Declare with `local` inside functions. Separate declaration from assignment when capturing exit codes: `local output; output="$(cmd)"`.
- **Comments**: Only for non-obvious logic. Do not add trivial comments.
- **Indentation**: 2 spaces, no tabs.

## Security Rules

- **No `eval`**: Do not use `eval`. If absolutely unavoidable, add a comment explaining why and what input has been validated.
- **Temporary files**: Always create with `mktemp`. Clean up in a `trap cleanup EXIT` handler. Never use predictable paths like `/tmp/myscript.tmp`.
- **Input validation**: Validate user-supplied arguments before use. Reject unexpected characters with pattern matching.
- **Guard `rm -rf`**: Never write `rm -rf "$dir"` without first verifying that `$dir` is non-empty and expected. Prefer `rm -rf "${dir:?}"` to abort on unset variables.
- **Avoid piping untrusted URLs to shell**: Do not use `curl ... | bash` patterns. Download first, inspect, then execute.
- **Privilege checks**: If the script requires root or specific permissions, check early and fail fast with a clear error message.

## Portability

When the script must work on both macOS and Linux, watch for these common incompatibilities:

| Area | macOS (BSD) | Linux (GNU) | Workaround |
|---|---|---|---|
| `sed` in-place | `sed -i '' 's/.../g'` | `sed -i 's/.../g'` | Use `sed -i.bak ... && rm *.bak`, or detect OS |
| `date` relative | No `-d` flag | `date -d '1 day ago'` | Use `date -v-1d` on macOS or `perl`/`python` |
| `readlink -f` | Not supported | Works | Use `cd "$(dirname "$0")" && pwd` pattern instead |
| `grep -P` (PCRE) | Not supported | Works | Use `grep -E` or `awk` |
| `mktemp` template | `mktemp -t prefix` | `mktemp prefix.XXXXXX` | Use `mktemp "${TMPDIR:-/tmp}/prefix.XXXXXX"` |
| `xargs -r` | Not supported | Skips empty input | Pipe through `grep .` before `xargs`, or check input |

If strict POSIX `sh` compatibility is required, additionally avoid: `[[ ]]`, arrays, `local` (non-POSIX but widely supported), process substitution `<()`, and `{a..z}` brace expansion.

## Notes

- Remove unused template sections. A 10-line script does not need `getopts` or `usage`.
- When modifying an existing script, follow the existing style rather than imposing the template.
- When the user says "shell script" without specifying a shell, default to bash.
- When fixing ShellCheck warnings, explain the reason to the user, not just the fix.

## References

- [GNU Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html)
- [Google Shell Style Guide](https://google.github.io/styleguide/shellguide.html)
- [ShellCheck](https://www.shellcheck.net/)
- [Greg's Wiki — BashPitfalls](https://mywiki.wooledge.org/BashPitfalls)
- [Greg's Wiki — BashFAQ](https://mywiki.wooledge.org/BashFAQ)
- [Greg's Wiki — BashGuide](https://mywiki.wooledge.org/FullBashGuide)

From shmokmt/agent-stuff, Apache-2.0.

Signals

Stars
0
Forks
0
Open issues
0
Last commit (this folder)
Mar 1, 2026 (8 months ago)

From GitHub (fetched Oct 24, 2026).

Details

License
Apache-2.0
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from shmokmt

Done for you

Rather not set it up yourself?

Chalk Labs scopes, builds and runs agents for your team: the task audit, the eval harness, the integrations and the weekly ops report. Fixed scope, priced before we start.

See how we build AI agents →Read the case studies

Chalk Labs is a two-founder AI and growth studio. We build AI agents, CRMs and MVPs for Web3 and AI companies, and have shipped 50+ product and token launches.

launches shipped
50+
launches shipped
media placements
500+
media placements
founders on every account
2
founders on every account

Press we've placed: Forbes · CoinDesk · Decrypt · Cointelegraph · The Block

Rahil Jain, Co-founder, Chalk Labs. Ex-Goldman Sachs engineer. You talk to the person who builds it.

Get bash-script running in your business

Tell us what you want to automate. A founder replies within one working day with a plan, not a pitch deck.

Goes to the Chalk Labs founders, not the newsletter. Privacy

Need help deploying bash-script?

CallGet help
Cite this page

bash-script skill. Agent Mart. https://agentmart.dev/skills/shmokmt-bash-script (last updated 2026-10-24)