Last active
November 10, 2017 11:32
-
-
Save Frogli/9e15864b3eacc6d403ac85e9f91e0ad2 to your computer and use it in GitHub Desktop.
Bash: Saving and loading variables from File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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