google-shell-style-guide | Skill Performance & Reviews | TopRankSkills

TopRank Skills

Home / Skills / tools / google-shell-style-guide

google-shell-style-guide

maintained by pproenca

star 0 account_tree 0 verified_user MIT License
bolt View GitHub

name: Google Shell Style Guide description: This skill should be used when the user asks to "refactor shell script", "fix bash style", "review shell code", "apply Google style guide", "improve shell script", mentions "shellcheck", or discusses bash/shell coding standards and best practices. allowed-tools: Read, Edit, Bash, Glob, Grep, mcp__plugin_serena_serena, mcp__plugin_serena_serena_*

Google Shell Style Guide

Comprehensive guidance for writing shell scripts following Google's Shell Style Guide.

Core Principles

  1. Consistency - Follow established patterns within each script
  2. Safety - Quote variables, use [[ ]], check return values
  3. Readability - 2-space indents, 80-char lines, clear naming

Quick Reference

Pattern Use Avoid
Command substitution $(command) `command`
Conditionals [[ condition ]] [ condition ]
Arithmetic (( expression )) $[ expression ]
Variables "${var}" $var
Arrays "${array[@]}" ${array[*]}

Formatting Rules

Indentation

  • Use 2 spaces (never tabs)
  • Align case patterns with esac

Line Length

  • Maximum 80 characters
  • Break long pipelines before |
  • Use backslash for line continuation

Control Structures

# if/then on same line
if [[ condition ]]; then
  command
elif [[ other ]]; then
  other_command
else
  fallback
fi

# while/do on same line
while [[ condition ]]; do
  command
done

# case alignment
case "${var}" in
  pattern1)
    command
    ;;
  pattern2)
    other
    ;;
esac

Variable Handling

Always Quote

# Correct
echo "${var}"
rm "${file}"

# Wrong - word splitting risk
echo $var
rm $file

Use Braces

# Correct - clear boundaries
echo "${prefix}_suffix"

# Wrong - ambiguous
echo "$prefix_suffix"

Arrays for Commands

# Correct - handles spaces safely
local -a args=(--flag "value with spaces")
command "${args[@]}"

# Wrong - breaks on spaces
local args="--flag value with spaces"
command $args

Function Guidelines

Naming and Declaration

# lowercase_with_underscores
process_file() {
  local input_file="$1"
  # ...
}

Local Variables

my_function() {
  local result=""
  local -r CONSTANT="value"  # readonly local
  # ...
}

Return Values

  • Use return for status codes (0-255)
  • Use echo or global variable for data
  • Always check return values
if ! process_file "${input}"; then
  err "Processing failed"
  return 1
fi

Error Handling

Standard Error Function

err() {
  echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
}

Strict Mode

#!/bin/bash
set -euo pipefail

Cleanup with Traps

cleanup() {
  rm -f "${tmp_file:-}"
}
trap cleanup EXIT

Issue Severity Classification

When reviewing shell scripts, classify issues by severity:

Severity Description Examples
Critical Security risks, data loss potential Command injection, unquoted variables in rm, eval usage
Important Correctness issues, portability problems Missing error handling, bashisms in /bin/sh scripts
Minor Style violations, readability issues Inconsistent indentation, long lines

Anti-Patterns to Avoid

  1. Never use eval - Command injection risk
  2. Never use aliases in scripts - Use functions instead
  3. Avoid piping to while - Creates subshell, loses variable changes
  4. Avoid mapfile/readarray - Not available on macOS

Additional Resources

For detailed patterns, edge cases, and comprehensive examples:

  • references/detailed-guide.md - Complete style guide with all rules
  • references/anti-patterns.md - Comprehensive anti-pattern catalog with fixes

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!

Skill Details

GitHub Stars 0
GitHub Forks 0
Created Jan 2026
Last Updated 4 months ago
tools tools automation tools

Related Skills

specs-gen
chevron_right
glm-coding-agent
chevron_right
feature-dev
chevron_right
creating-pr
chevron_right
writing-skills
chevron_right

Build your own?

Join 12,000+ developers contributing to the Claude ecosystem.