Skip to content

Instantly share code, notes, and snippets.

@Frogli
Last active November 10, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Frogli/9e15864b3eacc6d403ac85e9f91e0ad2 to your computer and use it in GitHub Desktop.
Save Frogli/9e15864b3eacc6d403ac85e9f91e0ad2 to your computer and use it in GitHub Desktop.
Bash: Saving and loading variables from File
#!/bin/bash
#
# source: https://www.linuxquestions.org/questions/programming-9/bash-script-%96-reading-and-writing-variables-to-a-separate-file-365158/#post1861674
#
PREFIX=playback
saveVariables()
{
set | grep ^$PREFIX
}
loadVariables()
{
saveVariables | while read -r line; do
echo $1 $line
done
}
playback1=korn
playback2=offspring
playback3="green day"
# load variables
# ==============
. saved
loadVariables ORIGINAL
playback3=""
# save them
# =========
saveVariables > saved
# delete them
# ===========
unset playback1 playback2 playback3
loadVariables UNSET
echo UNSET playback1=$playback1 playback2=$playback2 playback3=$playback3
# restore them
# ============
. saved
loadVariables SAVED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment