Welcome to docs’s documentation!¶
Contents:
- VASST POSIX Shell and Bash Coding Style Guidelines
- Consider using Bash instead of the POSIX shell
- File Layout
- Provide usage/help information
- Consider providing a machine-readable “Features:” line
- Prefer
getoptsfor processing command-line arguments - Avoid using
$0in usage and version output - Use tabs, not spaces
- The POSIX shell is called
/bin/sh, Bash is called/bin/bash - Use an empty string for a false value, and ‘1’ for a true one
- Error messages, warnings, and diagnostic information go to the standard error stream
- Consider making diagnostic messages conditional on a command-line option or environment variable
- Use
snake_casefor both function and variable names - Use uppercase names for variables passed to or from other programs
- Use lowercase names for the program’s own variables
- Put as much of the logic as possible in functions
- Avoid global variables if possible
- Use local variables
- Always initialize local variables when declaring them
- ALWAYS, always, ALWAYS double-quote variable expansions!
- Exception: running another program with more than one variable argument
- Put quotes around the longest expression possible
- Use single quotes for strings that do not contain variable interpolations
- Use the shellcheck tool
- Use the shell’s errexit and pipefail modes
- Keep in mind that errexit mode may be disabled in some cases
- Always test the error modes that the program checks for
- Use command substitution on a line separate from a variable declaration
- Use the Programmer’s Toolbox
- Use the shell’s own constructs
- Do not be afraid of using temporary files
- Use
findinstead ofls - Use Bash’s features
- Use the -e option when passing patterns to grep, sed, etc
- Use grep’s -q and -x options as appropriate
- Use text utilities in the optimal way
- ALWAYS, always, ALWAYS double-quote variable interpolations!
- Use two dashes as a delimiter between optional and positional arguments
- Use
printfinstead ofecho, especially for strings starting with a variable - Use temporary files (or directories) instead of files with predictable names
- Do NOT write parsers for complicated input data (JSON, HTML, INI-style files, etc)
- DO NOT SOURCE INI-STYLE FILES!
- Pass variables to tools that support that, e.g.
awkorjq - Use null-terminated strings for communication between programs if possible
- Use
find -execinstead offind | xargs