Skip to content

Commit e97a1cf

Browse files
Shell scripting instructions for bash, sh, zsh, and other shells (#389)
* shell scripting instructions Signed-off-by: Brooke Hamilton <[email protected]> * parsing guidelines Signed-off-by: Brooke Hamilton <[email protected]> --------- Signed-off-by: Brooke Hamilton <[email protected]>
1 parent 7af6193 commit e97a1cf

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

docs/README.instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Team and project-specific instructions to enhance GitHub Copilot's behavior for
106106
| [Rust MCP Server Development Best Practices](../instructions/rust-mcp-server.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Frust-mcp-server.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Frust-mcp-server.instructions.md) | Best practices for building Model Context Protocol servers in Rust using the official rmcp SDK with async/await patterns |
107107
| [Secure Coding and OWASP Guidelines](../instructions/security-and-owasp.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fsecurity-and-owasp.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fsecurity-and-owasp.instructions.md) | Comprehensive secure coding instructions for all languages and frameworks, based on OWASP Top 10 and industry best practices. |
108108
| [Self-explanatory Code Commenting Instructions](../instructions/self-explanatory-code-commenting.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fself-explanatory-code-commenting.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fself-explanatory-code-commenting.instructions.md) | Guidelines for GitHub Copilot to write comments to achieve self-explanatory code with less comments. Examples are in JavaScript but it should work on any language that has comments. |
109+
| [Shell Scripting Guidelines](../instructions/shell.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fshell.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fshell.instructions.md) | Shell scripting best practices and conventions for bash, sh, zsh, and other shells |
109110
| [Spec Driven Workflow v1](../instructions/spec-driven-workflow-v1.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fspec-driven-workflow-v1.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fspec-driven-workflow-v1.instructions.md) | Specification-Driven Workflow v1 provides a structured approach to software development, ensuring that requirements are clearly defined, designs are meticulously planned, and implementations are thoroughly documented and validated. |
110111
| [Spring Boot Development](../instructions/springboot.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fspringboot.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fspringboot.instructions.md) | Guidelines for building Spring Boot base applications |
111112
| [SQL Development](../instructions/sql-sp-generation.instructions.md)<br />[![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fsql-sp-generation.instructions.md)<br />[![Install in VS Code Insiders](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://aka.ms/awesome-copilot/install/instructions?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fsql-sp-generation.instructions.md) | Guidelines for generating SQL statements and stored procedures |

instructions/shell.instructions.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
description: 'Shell scripting best practices and conventions for bash, sh, zsh, and other shells'
3+
applyTo: '**/*.sh'
4+
---
5+
6+
# Shell Scripting Guidelines
7+
8+
Instructions for writing clean, safe, and maintainable shell scripts for bash, sh, zsh, and other shells.
9+
10+
## General Principles
11+
12+
- Generate code that is clean, simple, and concise
13+
- Ensure scripts are easily readable and understandable
14+
- Add comments where helpful for understanding how the script works
15+
- Generate concise and simple echo outputs to provide execution status
16+
- Avoid unnecessary echo output and excessive logging
17+
- Use shellcheck for static analysis when available
18+
- Assume scripts are for automation and testing rather than production systems unless specified otherwise
19+
- Prefer safe expansions: double-quote variable references (`"$var"`), use `${var}` for clarity, and avoid `eval`
20+
- Use modern Bash features (`[[ ]]`, `local`, arrays) when portability requirements allow; fall back to POSIX constructs only when needed
21+
- Choose reliable parsers for structured data instead of ad-hoc text processing
22+
23+
## Error Handling & Safety
24+
25+
- Always enable `set -euo pipefail` to fail fast on errors, catch unset variables, and surface pipeline failures
26+
- Validate all required parameters before execution
27+
- Provide clear error messages with context
28+
- Use `trap` to clean up temporary resources or handle unexpected exits when the script terminates
29+
- Declare immutable values with `readonly` (or `declare -r`) to prevent accidental reassignment
30+
- Use `mktemp` to create temporary files or directories safely and ensure they are removed in your cleanup handler
31+
32+
## Script Structure
33+
34+
- Start with a clear shebang: `#!/bin/bash` unless specified otherwise
35+
- Include a header comment explaining the script's purpose
36+
- Define default values for all variables at the top
37+
- Use functions for reusable code blocks
38+
- Create reusable functions instead of repeating similar blocks of code
39+
- Keep the main execution flow clean and readable
40+
41+
## Working with JSON and YAML
42+
43+
- Prefer dedicated parsers (`jq` for JSON, `yq` for YAML—or `jq` on JSON converted via `yq`) over ad-hoc text processing with `grep`, `awk`, or shell string splitting
44+
- When `jq`/`yq` are unavailable or not appropriate, choose the next most reliable parser available in your environment, and be explicit about how it should be used safely
45+
- Validate that required fields exist and handle missing/invalid data paths explicitly (e.g., by checking `jq` exit status or using `// empty`)
46+
- Quote jq/yq filters to prevent shell expansion and prefer `--raw-output` when you need plain strings
47+
- Treat parser errors as fatal: combine with `set -euo pipefail` or test command success before using results
48+
- Document parser dependencies at the top of the script and fail fast with a helpful message if `jq`/`yq` (or alternative tools) are required but not installed
49+
50+
```bash
51+
#!/bin/bash
52+
53+
# ============================================================================
54+
# Script Description Here
55+
# ============================================================================
56+
57+
set -euo pipefail
58+
59+
cleanup() {
60+
# Remove temporary resources or perform other teardown steps as needed
61+
if [[ -n "${TEMP_DIR:-}" && -d "$TEMP_DIR" ]]; then
62+
rm -rf "$TEMP_DIR"
63+
fi
64+
}
65+
66+
trap cleanup EXIT
67+
68+
# Default values
69+
RESOURCE_GROUP=""
70+
REQUIRED_PARAM=""
71+
OPTIONAL_PARAM="default-value"
72+
readonly SCRIPT_NAME="$(basename "$0")"
73+
74+
TEMP_DIR=""
75+
76+
# Functions
77+
usage() {
78+
echo "Usage: $SCRIPT_NAME [OPTIONS]"
79+
echo "Options:"
80+
echo " -g, --resource-group Resource group (required)"
81+
echo " -h, --help Show this help"
82+
exit 0
83+
}
84+
85+
validate_requirements() {
86+
if [[ -z "$RESOURCE_GROUP" ]]; then
87+
echo "Error: Resource group is required"
88+
exit 1
89+
fi
90+
}
91+
92+
main() {
93+
validate_requirements
94+
95+
TEMP_DIR="$(mktemp -d)"
96+
if [[ ! -d "$TEMP_DIR" ]]; then
97+
echo "Error: failed to create temporary directory" >&2
98+
exit 1
99+
fi
100+
101+
echo "============================================================================"
102+
echo "Script Execution Started"
103+
echo "============================================================================"
104+
105+
# Main logic here
106+
107+
echo "============================================================================"
108+
echo "Script Execution Completed"
109+
echo "============================================================================"
110+
}
111+
112+
# Parse arguments
113+
while [[ $# -gt 0 ]]; do
114+
case $1 in
115+
-g|--resource-group)
116+
RESOURCE_GROUP="$2"
117+
shift 2
118+
;;
119+
-h|--help)
120+
usage
121+
;;
122+
*)
123+
echo "Unknown option: $1"
124+
exit 1
125+
;;
126+
esac
127+
done
128+
129+
# Execute main function
130+
main "$@"
131+
132+
```

0 commit comments

Comments
 (0)