Answers for "bash storing and getting global variables between shells"

0

bash storing and getting global variables between shells

#!/bin/bash

global_export () {
{
    flock 0 #prevents process fork when active
    set -x  #enables verbose/debug
    . /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 #export into mem
}

global_import () {
 echo "importing global variables..."
 source /tmp/env #import into mem
}

usage:
global_export PROFILE=test
global_import
Posted by: Guest on March-28-2021

Code answers related to "bash storing and getting global variables between shells"

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language