Hi I have executed a function and I want to keep that values saved for further use . So what is best way to save multiple variables .My function is
test()
{
key1='first key'
key2='second key'
key3='third key'
}
Eventually if you want to save variables to a file and reuse them later:
#!/usr/bin/env bash
writekeys() {
local key1='first key'
local key2='second key'
local key3='third key'
# Save variables to file
typeset -p key1 key2 key3 >keys.sh
}
function2() {
. keys.sh # Load variables from file
printf 'key1=%s\n' "$key1"
printf 'key2=%s\n' "$key2"
printf 'key3=%s\n' "$key3"
}
writekeys
function2
. keys.sh; key1='newvalue'; typeset -p key1 key2 key3 >keys.shIf you want to reuse these variables, You could define these variables globally before defining your functions:
#/bin/bash
key1='first key'
key2='second key'
key3='third key'
func1()
{
echo $key1
}
func2()
{
echo $key2
key1="$key3"
}
now try executing the functions:
$ func1
first key
$ func2
second key
$ func1
third key
bash -x your-script to analyze what happens.
testto invoke the function, thenecho $key1shows you the value.