Developing bash scripts
Provide guidelines on how to develop bash shell scripts that are compliant with project standards. Should be used when generating, modifying, or reviewing bash scripts.From its SKILL.md
npx -y skills add brlin-tw/developing-bash-scriptsAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
5.7 KB, ~1.3k tokens by cl100k_base, as published. Nobody here has run it
The developing-bash-scripts agent skill
Provide guidelines on how to develop bash shell scripts that are compliant with project standards. It should be used when generating, modifying, or reviewing bash scripts.
Bash coding style
The following coding style guidelines should be followed when writing Bash scripts:
Shebang and file header
-
Use
#!/usr/bin/env bashas the shebang line for portability across different environments. -
Use the following file header format for all scripts:
#!/usr/bin/env bash # One line description of the script's purpose # # Copyright <year> <copyright holder name> <copyright holder contact> # _SPDX License Identifier Tag_: _SPDX license identifier_Replace
<year>,<copyright holder name>,<copyright holder contact>, and<SPDX license identifier>with the appropriate values derived from the project's Git author information and license. If not sure, consult the human for the correct values.Human may insert additional comment sections between the description and the copyright/license information which should be kept intact. The copyright/license information should always be the last comment in the file header.
Parameter naming
- Use lowercase letters and underscores for variable names (e.g.,
my_variable) to enhance readability. - Only use uppercase letters for variables whose values are sourced from the environment.
Parameter expansion
Use ${var} instead of $var for variable references to improve readability and avoid ambiguity.
This also applies to positional parameters, e.g., use ${1} instead of $1.
Message reporting
- If the program's output is intended to be used as input for other programs, avoid implementing non-error messages at all.
- Use
printfinstead ofechofor formatted output. - Prepend log level tags in the following format(except for help text):
Info:Warning:Error:FATAL:DEBUG:
Linting
Use ShellCheck for linting.
Defensive interpreter behavior
The following shell options should be set at the beginning of each script to ensure robust error handling:
set -o errexit # Exit on most errors (see the manual)
set -o nounset # Disallow expansion of unset variables
Do not set pipefail.
If the script contains functions, also include:
set -o errtrace # Ensure the error trap is inherited
Conditional constructs
-
When using
if...elseconstructs, always check the incorrect condition first. For example:if ! is_port_valid "${user_input}"; then printf \ 'Error: Invalid port number, please try again.\n' \ 1>&2 return 1 else # Do something when the condition is expected fi -
ALWAYS use the
testshell built-in for conditional expressions. For example:- DO NOT use
if [ -f "file" ], useif test -f "file"instead. - DO NOT use
if [[ -f "file" ]], useif test -f "file"instead. Disregard whatever your conception of how modern the[[ ... ]]construct is.
The only exception of using the
[[ ... ]]construct is when matching regular expressions. When doing so always define a variable with theregex_prefix for holding the regex pattern instead of embedding the regex directly in the conditional expression. - DO NOT use
REPEAT: DO NOT use [[ ... ]] for conditional expressions. Use test instead
Apparently some of you unable to discourage yourselves from using the [[ ... ]] construct well enough so here's the reminder.
Pattern matching
-
Use the
[[ ... ]]construct for validating user inputs when applicable.Store the regex pattern in a
regex_prefix variable instead of embedding it in the conditional expression. For example:local regex_digits='^[[:digit:]]+$' if [[ "${user_input}" =~ ${regex_digits} ]]; then # Do something when the input is a number fi
Passing data to subprocesses
-
Using the Here Strings syntax (
<<<) is preferred when passing small amounts of data to subprocesses. For example:grep 'pattern' <<< "${data_variable}"
Functions
-
Use
function_name(){ ... }syntax for defining functions. Do not use thefunctionkeyword. -
Always use
localfor function-local variables. -
Do not use global variables inside functions. Instead, pass them as arguments.
-
Use the following pattern to retrieve function arguments:
local var="${1}"; shiftAlways use
${1}parameter expansion and appendshiftcommand even when the function only has one parameter. This allows cleaner diffs when adding or removing arguments. -
Validate input parameters at the beginning of functions
-
Always use
returnto return an exit status from functions. Only use theexitbuiltin in theinit/mainfunction as it is the main logic of the script. -
Place all non
init/mainfunctions after theinit/mainfunction in the script. This allows script readers to access the main script logic easily. -
Use imperative tense for function names.
Error Handling
- Always check the exit status of commands and handle errors appropriately, don't rely solely on the ERR trap.
- Do not use AND/OR lists syntax.
Script template
The template script script should be used for creation of new scripts.
For the following placeholders in the template:
_copyright_holder_name__copyright_holder_contact_
use the user.name and user.email from git config respectively.
What ships with it: 27 files
97.5 KB alongside SKILL.md, 11 of them executable
LICENSES/
- CC-BY-SA-4.0.txt17.9 KB
- WTFPL.txt432 B
continuous-integration/
- create-gitlab-release.shruns2.1 KB
- do-static-analysis.install-system-deps.shruns7.0 KB
- do-static-analysis.shruns2.2 KB
- generate-build-artifacts.install-system-deps.shruns3.3 KB
- generate-build-artifacts.shruns3.0 KB
- generate-release-description.shruns4.3 KB
- patch-github-actions-sudo-security-policy.shruns4.2 KB
- sudoers.d/90_allow_github_actions_default_envvars.sudoers704 B
- sudoers.d/README.md365 B
- upload-gitlab-generic-packages.shruns1.8 KB
dev-assets/
- deploy-development-environment.shruns4.4 KB
- README.md93 B
- .editorconfig1.5 KB
- functions.shruns26.3 KB
- .gitattributes621 B
- .gitignore1.7 KB
- .gitlab-ci.yml1.8 KB
- .gitmodules230 B
- .markdownlint.yml1.4 KB
- .pre-commit-config.yaml2.3 KB
- README.md608 B
- REUSE.toml496 B
- .shellcheckrc403 B
- template.shruns2.5 KB
- .yamllint5.7 KB