Catalogue / shell-scripts
shell-scripts
MITSignedStandards for Bash and PowerShell scripts and one-liners: structure, strict error handling, argument parsing, validation, and output streams. Use when writing, refactoring, or reviewing a .sh/.bash or .ps1 script or a terminal one-off.--- name: shell-scripts description: >- Standards for Bash and PowerShell scripts and one-liners: structure, strict error handling, argument parsing, validation, and output streams. Use when writing, refactoring, or reviewing a .sh/.bash or .ps1 script or a terminal one-off. ---
Shell scripts
Write shell code in one of two modes, in the language the target system runs.
Pick the mode
| Mode | Use when | Output | | --- | --- | --- | | OneOff | A one-liner, quick command, ad-hoc check, or terminal snippet | Snippet: Bash 1-5 lines (max 10), PowerShell 1 line (max 5). No file unless asked. | | FullScript | A script file, reusable CLI tool, or multi-step workflow | One file built from the template |
Default to FullScript when the ask is ambiguous.
Pick the language
- Follow the user's ask or the repository's existing scripts.
- Otherwise use Bash for Linux and macOS targets, and PowerShell 7 for Windows targets. When a script must run on both, write both, or suggest Python instead.
- Bash scripts assume Bash 4+. macOS ships 3.2 at
/bin/bash, so say so when a script uses Bash 4 features.
Rules for both languages
- Validate all inputs before any work. Use guard clauses and keep nesting to three levels.
- Send primary output to stdout. Send logs, status, warnings, and errors to stderr (PowerShell:
Write-Verbose,Write-Information,Write-Error; neverWrite-Hostfor diagnostics). - Fail fast. Never ignore a non-zero exit or swallow an error without a comment saying why it is safe.
- Do not hard-code paths or configurable values. Use parameters or named constants.
- Keep business logic out of top-level code. Top level holds only declarations, setup, and one call to
main(Main). - Give errors enough to act on: what was being done, what failed, and how to fix it.
- In a OneOff that deletes or changes things, show a preview first (
-print,--dry-run,-WhatIf) and leave the destructive form for the user to run. - Write temp files with
mktemporNew-TemporaryFile, and remove them on exit.
FullScript essentials
Bash (bash.md, template template.sh):
#!/usr/bin/env bash, a header comment (description, usage, changelog), thenset -euo pipefail.readonlyconstants,localvariables in functions, integer0/1flags, namedE_*exit codes.die,log_*,usage,parse_args(usinggetoptswith a leading:), andmain, defined last.- Traps for
EXIT(idempotent cleanup),INT(exit 130), andTERM(exit 143).
PowerShell (powershell.md, template template.ps1):
- Comment-based help first (
.SYNOPSIS,.DESCRIPTION,.PARAMETERfor each parameter,.EXAMPLE,.NOTES), then[CmdletBinding()],param(), andSet-StrictMode -Version Latest. - Validation attributes on every parameter. Business logic in documented classes. A
Mainfunction, defined last and invoked once. - Return objects, not formatted text. Use
SupportsShouldProcessfor destructive actions. #regionblocks for Constants, Classes, Helpers, and Main.
Checklist
- [ ] Mode and language match the ask.
- [ ] FullScript follows the template's section order.
- [ ] Bash passes
shellcheckandbash -n. PowerShell passesInvoke-ScriptAnalyzerwhen available. - [ ]
-h/Get-Helpshows usage. Invalid arguments exit with the usage error code.
References
- bash.md: read when writing or reviewing a Bash FullScript.
- powershell.md: read when writing or reviewing a PowerShell FullScript.
- template.sh, template.ps1: copy to start a FullScript.
| File | Size | SHA-256 |
|---|---|---|
| INTENT.md | 2.1 kB | afddecf8f245 |
| README.md | 857 B | 69a7c0ef9407 |
| SKILL.md | 3.7 kB | a11f08dea166 |
| SKILVAULT_SOURCE.json | 1.7 kB | 38731457d180 |
| THIRD_PARTY_NOTICES.md | 1.1 kB | cc455b6f63ed |
| assets/template.ps1 | 1.7 kB | fe6b08ca48a8 |
| assets/template.sh | 3.7 kB | 397f2e7448d7 |
| references/bash.md | 3.6 kB | 240d371b8c8d |
| references/powershell.md | 2.2 kB | 0f626c04b729 |
Published verbatim from the pinned commit. Nothing is rewritten.
| Version | Pinned commit | Signed | |
|---|---|---|---|
| 0.1.0 Current | 6aa8826ce406 | 29 Nov 2026 |
Locking a skill to one version needs Pro.