bash storing and getting global variables between shells code example
Example: bash storing and getting global variables between shells
#!/bin/bash
global_export () {
{
flock 0
set -x
. /tmp/env
[ $# -eq 0 ] || export "$@"
echo "$@"
KEY=`echo "$@" | awk '{split($0,a,"="); print a[1]}'`
VAR_IN_FILE=`cat /tmp/env | grep "$KEY"`
(grep -q "$KEY" /tmp/env || echo "$@" >> /tmp/env) && grep -rl "$KEY" /tmp/env | xargs sed -i "s/$VAR_IN_FILE/$@/g"
} < /tmp/env
source /tmp/env
}
global_import () {
echo "importing global variables..."
source /tmp/env
}
usage:
global_export PROFILE=test
global_import