I have a Bash function that contains a variable that I need to access from another function. How do I do this?
Here is an example:
#!/bin/bash
function run_query() {
sleep 5
local -g DATA=$(curl "http://api.dictionary.words/?definition=program")
}
function display_data() {
printf "%s\n" "$DATA"
}
run_query &
display_data
The first function runs every 5 seconds and the second function runs every second because I did not want to query the API every second.
Then, you can query every 5 seconds
i=0
while :
do
((i++ % 5 == 0)) && data=$(curl "http://api.dictionary.words/?definition=program")
printf '%s\n' "$data"
sleep 1
done