Skip to content
Back to skills

Refactor Shell

ASecurity

Shell norms: pick the dialect by where the script runs (strict POSIX for portable ones, bash for a gate), quote every expansion, and keep the error path explicit. Use when editing shell scripts.

  • 4 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added October 1, 2026
ai-agentspythonshellbashrailsrefactoringperformance

Security analysis

A100/100

Scanned October 1, 2026

npx -y skills add Univers42/claude-deal-with-the-devil --skill refactor-shell --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Refactor Shell?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Refactor Shell
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/univers42-refactor-shell/badge)](https://www.skillsdirectory.com/skills/univers42-refactor-shell)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
# GENERATED by 'devil export gemini' from the kit's Claude-format sources. Do not edit; run 'devil export gemini' instead.
name: refactor-shell
description: >
  Shell norms: pick the dialect by where the script runs (strict POSIX for portable ones,
  bash for a gate), quote every expansion, and keep the error path explicit. Use when
  editing shell scripts.
paths:
  - "**/*.sh"
  - "**/*.bash"
user-invocable: false
metadata:
  stage: rule
  since: "1.0.0"
---

# Shell Refactoring

## Pick the dialect by where the script runs

- **Portable scripts** (an installer, anything run by `/bin/sh` on a host you do not
  control): strict POSIX, the section below.
- **Gates and tools** (this kit's `tools/`, `bin/`, `tests/`, a project's CI helpers):
  bash is allowed. Shebang `#!/usr/bin/env bash`, then `set -uo pipefail`. Leave `-e`
  off where a failing command is data (a gate that found a problem must still report
  it); a script that should stop at the first error adds it. Arrays, `[[ ]]` and
  `local` are fine. A sourced library sets no options at all (`tools/README.md`).

## Strict POSIX compliance (portable scripts)

- No bashisms — no [[]], no arrays, no (( )), no ${var/pat/rep}
- Shebang: #!/bin/sh
- No unset variable access — set -u compatible
- Test with dash, not just bash

## Both dialects

- Quote every variable expansion: "$var" not $var
- Use command -v over which
- printf over echo for anything non-trivial

## Structure

- Max 25 lines per function — keep them short
- Functions at top, execution at bottom
- Local variables via local keyword or subshell isolation
- Cleanup via trap — every temp file cleaned on EXIT

## After refactoring

- `shellcheck`: zero warnings; it reads the dialect from the shebang
  (`shellcheck -s sh` to force POSIX on a script without one)
- `shfmt -d -i 2`: no diff
- Runs correctly under every shell you target, not just your default

## Shell-specific ladder extensions

- Rung 2: shell builtins over external commands (`${#var}` over `wc -c`, `${var%.*}` over `basename`).
- Rung 3: an awk one-liner over a Python script for text processing.
- Rung 4: already have `jq`? Use it for JSON — don't `sed`/`grep`.
- Rung 5: pipeline over temp file. Always.
- No function wrapper around a single command.

## Shell performance guardrails

- Ladder says "builtin" but:
  - shell loop over lines? A single `awk`/`sed` instead — one process beats N fork+execs.
  - `$(cat file)`? Use `< file` redirection.
  - `grep | awk | sed` pipeline? Usually one `awk` does all three.
- Ladder says "one-liner" but:
  - command substitution in a `while` loop? Forks per iteration — process in bulk.
- Minimize subshells: `$()` forks, variable assignment doesn't.
- Minimize pipe stages: each is a fork + FD pair.
- Heredoc over `echo` piped to a command.
- `exec` for the final command in a script (no useless parent shell lingering).

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.