Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash constants readonly #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ prefer to make the argument mandatory and pass `.` instead.
* Constant variables use `SCREAMING_SNAKE_CASE` names.

This means variables that are assigned once and then never modified, and that only depend on
other constant variables. The exception to "never modified" is where multiple statements are
used to compute the content of the "constant" (see `COMPILE_FLAGS` in example at the end).
other constant variables.

* Non-constant variables use `snake_case` names.

Expand All @@ -70,6 +69,10 @@ prefer to make the argument mandatory and pass `.` instead.
Environment variables and arguments to the script are considered constants. But arguments in
functions are not constants.

Constant variables should always have `readonly` applied to them. Non-constant variables can
also have `readonly` applied to them assuming they are never modified and using `readonly` is
not too verbose.

## Conditions and branching

Use `[[` for conditions, not `[`.
Expand Down Expand Up @@ -213,10 +216,10 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# Base compile flags valid all the time
COMPILE_FLAGS="disable-foo enable-bar"
compile_flags="disable-foo enable-bar"
# Platform specific compile flags
if [[ "$(uname -s)" != "Darwin" ]]; then
COMPILE_FLAGS+=" disable-apple"
compile_flags+=" disable-apple"
fi

# Script arguments count as constants
Expand Down