a small library to provide a form of global variables in a bash shell
The library can be taken from the scripts folder, put in your folder with scripts, and dot-source in your scripts.
. ./.globals.bashThe functions provided:
-
globalis used to set a global variable. It is used like one uses theexportstatement or thelocalstatement in a function.global MY_VAR="some value"- The variable is set in the current environment.
- The variable is exported in the current environment, so it is available in the environments of all future child-shells.
- The variable is set in the environments of the chain of parent-shells that used
callto eventually run this script. - The variable is exported in the environments of these parent-shells, so it is available in the environments of all future child-shells of these parent-shells.
-
callis used to execute a script, pickup all global variables set by that script and itscall-ed child-scripts, and inject these global variables in the current environment.call ./my-script
callusesbashto execute the script- The exitcode of the executed script is returned in
$? - A global variable
$LASTEXITCODEis set to the$?value of the lastcall-ed script - A global variable
$GLOBALS_INJECTis reserved to communicate the dynamically created filedescriptor of the inject-stream between calling and called scripts. This inject-stream is used to send the required information to set global variables, from the called script back to the calling script. This inject-stream is only open for as long as the called script is running.
Let's make three scripts
-
test-globals-1.bashcallstest-globals-2.bash. ./.globals.bash echo "### test-globals-1.bash" echo "" call ./test-globals-2.bash echo "\$?=$?" echo "" echo "### test-globals-1.bash" echo "\$LASTEXITCODE=$LASTEXITCODE" echo "\$MY_VAR=$MY_VAR" echo "###" echo ""
-
test-globals-2.bashcallstest-globals-3.bash. ./.globals.bash echo "### test-globals-2.bash" echo "" call ./test-globals-3.bash echo "\$?=$?" echo "" echo "### test-globals-2.bash" echo "\$LASTEXITCODE=$LASTEXITCODE" echo "\$MY_VAR=$MY_VAR" echo "###" echo "" exit $LASTEXITCODE
-
test-globals-3.bashset a global variable. ./.globals.bash echo "### test-globals-3.bash" echo "" global MY_VAR="some value" echo "### test-globals-3.bash" echo "\$LASTEXITCODE=$LASTEXITCODE" echo "\$MY_VAR=$MY_VAR" echo "###" echo "" exit 42
Now run
./test-globals-1.bashThe result is:
### test-globals-1.bash
### test-globals-2.bash
### test-globals-3.bash
### test-globals-3.bash
$LASTEXITCODE=
$MY_VAR=some value
###
$?=42
### test-globals-2.bash
$LASTEXITCODE=42
$MY_VAR=some value
###
$?=42
### test-globals-1.bash
$LASTEXITCODE=42
$MY_VAR=some value
###
ℹ️
The scripts folder contains a more elaborate version of the above scripts.
- use the same techniques to return results to the caller of a script