Shell script best practices
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
Install this rule for wispbit
Quick Install
Run this one command to automatically install the rule:
Manual install
Copy the rule
---
include: *.sh
---
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
Add the rule into your project
.wispbit/rules/shell-best-practices.md
Install this rule for Coderabbit
Copy the configuration below and add it to your repository as .coderabbit.yml
in your project root.
reviews:
path_instructions:
- path: "*.sh"
instructions: |
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
Install this rule for Greptile
Greptile rules can be added through the web interface. Please see this documentation for details on how to add custom rules and context.
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
File Path Patterns:
Install this rule for GitHub Copilot
Copilot instructions can be added through the interface. See the documentation for details on how to create coding guidelines.
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
File Path Patterns:
Install this rule for Graphite Diamond
Diamond custom rules can be added through the interface. See the documentation for details on how to create custom rules.
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
File Path Patterns:
Use with Cline
Copy the rule below and ask Cline to review your code using this rule
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
Use with OpenAI Codex
Copy the rule below and ask OpenAI Codex to review your code using this rule
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
Use with Cursor
Copy the rule below and ask Cursor to review your code using this rule
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```
Use with Claude Code
Copy the rule below and ask Claude Code to review your code using this rule
Follow these shell script best practices when writing or modifying bash scripts:
1. Use `#!/usr/bin/env bash` instead of `#!/bin/bash` for better portability
Bad:
```bash
#!/bin/bash
echo "Hello World"
```
Good:
```bash
#!/usr/bin/env bash
echo "Hello World"
```
2. Use `${variable}` syntax for all variable references
Bad:
```bash
name="Alice"
echo "Hello $name"
```
Good:
```bash
name="Alice"
echo "Hello ${name}"
```
3. Use double quotes around variable expansions to prevent word splitting and globbing
Bad:
```bash
files=$(ls)
for file in $files; do
rm $file
done
```
Good:
```bash
files=$(ls)
for file in "${files}"; do
rm "${file}"
done
```
4. Use `[[` and `]]` instead of `[` and `]` for conditional expressions
Bad:
```bash
if [ -z "$input" ] || [ "$input" = "exit" ]; then
echo "Exiting..."
exit 0
fi
```
Good:
```bash
if [[ -z "${input}" || "${input}" = "exit" ]]; then
echo "Exiting..."
exit 0
fi
```
5. Use `printf '%s\n'` instead of `echo` for output with special characters
Bad:
```bash
output="Text with special chars: * & $HOME"
echo $output
```
Good:
```bash
output="Text with special chars: * & $HOME"
printf '%s\n' "${output}"
```
6. Use `readonly` for constants that should not be modified
Bad:
```bash
CONFIG_PATH="/etc/app/config.json"
LOG_DIR="/var/log/app"
```
Good:
```bash
readonly CONFIG_PATH="/etc/app/config.json"
readonly LOG_DIR="/var/log/app"
```
7. Use `|` as delimiter in `sed` commands when replacing paths containing slashes
Bad:
```bash
sed -i "s/${source_path}/${target_path}/" "${config_file}"
```
Good:
```bash
sed -i "s|${source_path}|${target_path}|" "${config_file}"
```