bash

Write secure, idiomatic Bash scripts with strict coding conventions and tooling standards.

Updated Jul 15, 2024
One-click install
npx skills add https://github.com/christian-deleon/dotfiles --skill bash-christian-deleon
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bash
Source: https://github.com/christian-deleon/dotfiles/tree/main/ai/skills/bash
Command: npx skills add https://github.com/christian-deleon/dotfiles --skill bash-christian-deleon

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires shellcheck, shfmt, bats-core, shellharden, and includes scripts (resource) and references (resource) and assets (resource) components.

What problem does it solve?

This Skill unit enhances Bash scripting practices, ensuring that scripts are modern, secure, and lint-clean.

Core Features & Use Cases

  • Modern Bash Scripting Guidelines: Encourages the use of features like set -Eeuo pipefail, shopt -s inherit_errexit, and shopt -s nullglob.
  • Error Handling & Cleanup: Outlines standard die() function and trap pair for resource cleanup and error handling.
  • Variables & Quoting: Emphasizes strict variable quoting and local declaration for function-scoped variables.
  • Control Flow: Recommends using [[ ]] over [ ], (( )) over expr, and case statements for string dispatch.
  • Security: Discusses avoiding eval, protecting against command injection, and performing atomic writes.
  • Project Layout: Provides best practices for organizing project files in bin/, lib/, and test/ directories.
  • Tooling: Recommends using shellcheck, shfmt, bats-core, and shellharden for script analysis, formatting, and testing.

Quick Start

Install and update your Bash scripting skill by running 'source /path/to/bash/skill.sh' and then follow the guidelines within.

Frequently Asked Questions about bash

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
What is the best way to write secure and modern Bash scripts?

Secure Bash scripting relies on strict conventions like `set -Eeuo pipefail` and `shopt -s inherit_errexit` to catch errors early. It also involves avoiding `eval` to protect against command injection and using trap pairs for reliable resource cleanup.

How do I handle errors and cleanup in shell scripting?

Error handling in Bash uses a standard `die()` function paired with a trap to catch failures and execute cleanup routines. This guarantees that temporary files and resources are removed even when scripts abort unexpectedly.

How do I format and lint Bash scripts using shellcheck and shfmt?

Linting Bash scripts with shellcheck identifies unsafe code patterns, while shfmt enforces consistent formatting. Together, they analyze scripts for security vulnerabilities and automatically format code to adhere to strict tooling standards.

Why should I use double brackets in Bash control flow?

Using `[[ ]]` over `[ ]` in Bash control flow prevents word splitting and pattern matching errors. It is the idiomatic standard for writing robust, modern shell scripts that handle string dispatch securely.

Does Bash scripting work with bats-core for unit testing?

Bash scripting works seamlessly with bats-core to execute automated unit tests. Organizing your project with a dedicated `test/` directory allows you to validate script behavior and ensure code quality before deployment.