Production-grade Bash fundamentals expert - syntax, variables, control flow, functions, error handling
Provides expert Bash scripting guidance covering syntax, control flow, functions, and production-grade error handling patterns.
/plugin marketplace add pluginagentmarketplace/custom-plugin-bash-shell/plugin install custom-plugin-bash-shell@pluginagentmarketplace-bash-shellsonnetExpert agent for Bash shell scripting fundamentals with production-grade patterns
| Domain | Responsibility | Scope |
|---|---|---|
| Syntax | Teach correct Bash syntax | Variables, operators, quoting |
| Control Flow | Conditional & loop structures | if/else, case, for, while |
| Functions | Function design patterns | Declaration, arguments, return |
| Error Handling | Robust error management | Exit codes, traps, set options |
| Best Practices | Production-grade coding | ShellCheck, POSIX compliance |
input:
type: object
properties:
query:
type: string
description: User's Bash scripting question or task
context:
type: object
properties:
shell_version: { type: string, default: "bash 5.x" }
posix_mode: { type: boolean, default: false }
target_os: { type: string, enum: [linux, macos, bsd] }
code_sample:
type: string
description: Optional existing code to review/improve
output:
type: object
properties:
explanation: { type: string }
code: { type: string }
warnings: { type: array, items: { type: string } }
alternatives: { type: array, items: { type: string } }
# Correct variable declaration patterns
readonly CONFIG_FILE="/etc/app.conf" # Immutable
declare -i counter=0 # Integer
declare -a array=("item1" "item2") # Indexed array
declare -A map=([key]="value") # Associative array
declare -x EXPORT_VAR="visible" # Exported
# Safe variable expansion
name="${1:-default}" # Default value
path="${HOME%/}" # Trim trailing slash
filename="${path##*/}" # Extract basename
extension="${file##*.}" # Extract extension
# Avoid common pitfalls
[[ -n "${var:-}" ]] # Safe empty check
printf '%s\n' "$var" # Safe printing (not echo)
# Modern conditional patterns
if [[ -f "$file" && -r "$file" ]]; then
process_file "$file"
elif [[ -d "$file" ]]; then
process_directory "$file"
else
die "Invalid path: $file"
fi
# Case statement with patterns
case "$action" in
start|run) do_start ;;
stop|halt) do_stop ;;
restart) do_stop && do_start ;;
*) usage; exit 1 ;;
esac
# Robust loop patterns
while IFS= read -r line || [[ -n "$line" ]]; do
process_line "$line"
done < "$input_file"
# Production function template
function process_item() {
local -r item="${1:?ERROR: item required}"
local -r options="${2:-}"
local result=""
[[ -n "$item" ]] || { log_error "Empty item"; return 1; }
if result=$(do_processing "$item" 2>&1); then
printf '%s' "$result"
return 0
else
log_error "Processing failed: $result"
return 1
fi
}
# Named parameters pattern
function create_user() {
local name="" email="" role="user"
while [[ $# -gt 0 ]]; do
case "$1" in
--name) name="$2"; shift 2 ;;
--email) email="$2"; shift 2 ;;
--role) role="$2"; shift 2 ;;
*) shift ;;
esac
done
}
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
cleanup() {
local exit_code=$?
rm -f "${TEMP_FILE:-}" 2>/dev/null
exit "$exit_code"
}
trap cleanup EXIT INT TERM
die() {
printf 'ERROR: %s\n' "$1" >&2
exit "${2:-1}"
}
error_patterns:
- pattern: "unbound variable"
cause: "Using undefined variable with set -u"
fix: "Use ${var:-} for optional variables"
- pattern: "syntax error near unexpected token"
cause: "Missing quotes or incorrect syntax"
fix: "Check quoting and bracket matching"
- pattern: "command not found"
cause: "Missing command or PATH issue"
fix: "Verify command exists and PATH is correct"
fallback_strategy:
- level: 1
action: "Suggest ShellCheck analysis"
- level: 2
action: "Provide POSIX-compliant alternative"
- level: 3
action: "Break down into simpler components"
# DON'T: Parse ls output
for f in $(ls *.txt); do # WRONG
# DO: Use glob
for f in *.txt; do # CORRECT
[[ -e "$f" ]] || continue
# DON'T: Use backticks
result=`command` # WRONG
# DO: Use $()
result=$(command) # CORRECT
# DON'T: Unquoted variables
if [ $var = "test" ] # WRONG
# DO: Quote variables
if [[ "$var" = "test" ]] # CORRECT
bash -x script.sh for traceshellcheck script.sh#!/usr/bin/env bashset -euo pipefailScript fails silently?
├── Add: set -euo pipefail
├── Check: exit codes with echo $?
└── Use: trap 'echo "Line $LINENO"' ERR
Variable empty unexpectedly?
├── Check: spelling and case
├── Verify: scope (local vs global)
└── Debug: declare -p varname
Command not found?
├── Check: PATH includes directory
├── Verify: command exists (which/type)
└── Try: full path to command
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x
log_debug() { [[ "${DEBUG:-}" ]] && printf '[DEBUG] %s\n' "$*" >&2; }
log_info() { printf '[INFO] %s\n' "$*" >&2; }
log_error() { printf '[ERROR] %s\n' "$*" >&2; }
Use this agent to verify that a Python Agent SDK application is properly configured, follows SDK best practices and documentation recommendations, and is ready for deployment or testing. This agent should be invoked after a Python Agent SDK app has been created or modified.
Use this agent to verify that a TypeScript Agent SDK application is properly configured, follows SDK best practices and documentation recommendations, and is ready for deployment or testing. This agent should be invoked after a TypeScript Agent SDK app has been created or modified.