|
| 1 | +--- |
| 2 | +name: c-bash |
| 3 | +description: make .sh files follow common C code conventions for readability and maintainability |
| 4 | +allowed-tools: bash |
| 5 | +--- |
| 6 | + |
| 7 | +All `.sh` files should follow `.claude/skills/bash-coding-standards.md` |
| 8 | + |
| 9 | +You MUST work on one file at a time, don't do bulk updates since these tend to break scripts subtly. |
| 10 | + |
| 11 | +When replacing `"${var}"` it is _not_ always safe to use `$var`, especially if the variable might contain spaces or unknown input. |
| 12 | + |
| 13 | +It is only safe to use the shortened `$var` syntax when the variable is known to be a simple string value, usually that means its set earlier in the script. |
| 14 | + |
| 15 | +It is also only safe to remove the `{}` if the use of this variable does not cause ambiguity, or if the text following it does not cause bash to interpret it as a different variable. |
| 16 | + |
| 17 | +After making changes to each file one at a time you must `bash -n` the changed script and then run the script to make sure it still works. |
| 18 | + |
| 19 | +## converting non-compliant code |
| 20 | + |
| 21 | +When asked to convert bash to to these conventions... |
| 22 | + |
| 23 | +Never do mass sed/perl replacements across many files at once. |
| 24 | + |
| 25 | +Each change must be read in context first. A variable that looks like a path may hold multi-line output, quoted strings, or JSON. The fix that is correct for one variable may silently corrupt another. |
| 26 | + |
| 27 | +Quotes are load-bearing — check usage before removing them |
| 28 | +"$var" is NOT just style. It is required when: |
| 29 | +- the variable may contain spaces (file names, URLs with query strings, response bodies) |
| 30 | +- the variable may contain newlines (curl response bodies, grep output) |
| 31 | +- the variable may contain glob characters (*, ?, [) |
| 32 | +- the variable is on the right-hand side of [[ ]] and could contain * or ? |
| 33 | + |
| 34 | +- Only remove quotes when you can see, from reading the script, that none of these apply. |
| 35 | +echo "$var" must stay quoted when var is a response body |
| 36 | +echo $var word-splits and glob-expands. echo "$var" preserves newlines. Any variable holding HTTP response bodies, JSON, or log output must keep quotes. |
| 37 | + |
| 38 | +Work iteratively — one file at a time |
| 39 | + |
| 40 | +1. Read the full file |
| 41 | +1. Identify violations |
| 42 | +1. Understand each variable's content and all its usages |
| 43 | +1. Make targeted changes |
| 44 | +1. Run bash -n and the test before moving to the next file |
| 45 | + |
| 46 | +Never use automated tools (sed/perl/python) to rename variables across files |
| 47 | + |
| 48 | +Variable names are not unique tokens — $body in one script is a response body requiring quotes; in another it is a simple path. Mass rename without reading context will introduce bugs. |
| 49 | +The rule is: remove ${braces} only, not quotes |
| 50 | + |
| 51 | +"${foo}" → "$foo" is usually safe (keeps quotes, removes unnecessary braces). |
| 52 | +If any code uses "${foo}_baa" changing to "$foo_bar" is not safe. |
| 53 | + |
| 54 | +"${foo}" → $foo is only safe after reading the specific usage. |
0 commit comments