Background
I am writing a function to append to a file in my $HOME directory called .bash.local.
➡️ .bash.local is sourced via .bash_profile.
However, I want to conditionally append to .bash.local if and only if the file does not already contain the contents of $BASH_CONFIGS.
Things to Keep in Mind
My operating system of choice is MacOS Mojave, thus certain versions of command line applications will be different (e.g. grep on a Mac is BSD grep and not GNU grep).
⚠️ append_to_bash_local()
append_to_bash_local() {
local LOCAL_BASH_CONFIG_FILE="$HOME"/.bash.local
declare -r BASH_CONFIGS="
# TOOL_NAME - TOOL_DESCRIPTION.
# Add tool configurations here
"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# If needed, add the necessary configs in the
# local shell configuration file.
if ! grep "^$BASH_CONFIGS" < "$LOCAL_BASH_CONFIG_FILE" &> /dev/null; then
# this block never runs, even if the contents of $BASH_CONFIG
# are NOT present in $HOME/.bash.local
fi
}