Reputation: 159
It is known that it is possible to make common variables used by several scripts available to each other by storing them in a common file.
The exchange of variable values between several scripts via a file on a hard disk, in contrast to an exchange via a variable held in RAM, leads to frequent write operations on a corresponding hard disk.
Software implementation of known way:
# write a var to a file
echo "var_01=Alfa" > var_file.sh
# set a new value to a var
sed -i 's/var_01=Alfa/var_01=Berta/' var_file.sh
# delete the value of a var and the var
sed -i 's/var_01=Alfa/""/' var_file.sh
# make a var readable to a script by Include var_file.sh
source ./var_file.sh
What would be a better practice, how to make a variable of a bash script, available to another bash script ?
Remark: Searched are:
Upvotes: 0
Views: 157
Reputation: 19615
With bash, use the declare
keyword to reliably (properly quote or escape special characters) save variable to file, even if it contains spaces or any special characters.
Eventually you can incorporate a save_vars
function into the var_file.sh
that you share with all your other scripts, to save and recover/share variables.
var_file.sh
#!/usr/bin/env bash
# Check file is sourced from a bash script or exit with error
if [ -z "$BASH_VERSION" ] || ! (return 0 2>/dev/null); then
printf 'This %s file is meant to be sourced by a Bash Script\n' "$0" >&2
exit 1
fi
# Function to save itself and variable names in arguments
# into the current file
# (here it is var_file.sh but could be whatever it is named)
save_vars() {
# save variables file
{
# Write self shebang and usage error handling
cat <<'EOF'
#!/usr/bin/env bash
if [ -z "$BASH_VERSION" ] || ! (return 0 2>/dev/null); then
printf 'This %s file is meant to be sourced by a Bash Script\n' "$0" >&2
exit 1
fi
EOF
# Save itself "${FUNCNAME[0]}" is the name of
# the current function here it is save_vars
declare -f "${FUNCNAME[0]}"
# Save variable names passed as arguments
declare -p "$@"
# Into itself file (self-modify or update)
} >"${BASH_SOURCE[0]}" 2>/dev/null
}
script1
:
#!/usr/bin/env bash
# load variables or exit with failure
. var_file.sh || exit 1
printf 'Hello I am %s\n' "${BASH_SOURCE[0]}"
# Assign value even with spaces and special chars
var_01=$'Alfa Beta Gamma \317\206'
var_02='Something with
a newline'
# Invoke the save_vars function that was imported
# from var_file.sh to save var_01 and var_02
save_vars var_01 var_02
script2
:
#!/usr/bin/env bash
# load variables or exit with failure
. var_file.sh || exit 1
printf 'Hello I am %s\n' "${BASH_SOURCE[0]}"
printf 'var_01 contains: %q\n' "$var_01"
printf 'var_02 contains: %q\n' "$var_02"
# set a new value to a var
var_01='Berta'
# delete var_02
unset var_02
# Invoke the save_vars function that was imported
# from var_file.sh to save var_01 only
# (var_02 does not need to be saved since it is deleted
# it will be removed/gone from the save)
save_vars var_01
script3
#!/usr/bin/env bash
# load variables or exit with failure
. var_file.sh || exit 1
printf 'Hello I am %s\n' "${BASH_SOURCE[0]}"
printf 'var_01 contains: %q\n' "$var_01"
printf 'var_02 contains: %q\n' "$var_02"
Upvotes: 4