Search code examples
bashfunctionvariableswait

Variables are not retained after using wait command with a function in BASH


I have to log measurements from a variety of sensors, some of which take quite a while. I want to obtain the readings from the sensors simultaneously in order to save time.

I have a function, foo, that reads some sensors and saves the values as variables, e.g. a variable called bar.

I am using the wait command to pause the script until the foo function is finished prior to making a HTTP get to send all the measurements to a server for logging.

#!/bin/bash

function foo {
    bar=$(command for reading sensor here)
    echo "bar is $bar"
}

foo & #this is to run the foo function as a background process.

temperature=$(command for measuring temperature here)
humidity=$(command for measuring humidity here)

wait %1 #this should pause the script until the function foo is complete.
echo "function foo has finished. bar is $bar"

curl -X GET -G (URL to server) -d bar=$bar -d temperature=$temperature -d humidity=$humidity

When I run the script, the output is as follows:

bar is 1234

function foo has finished. bar is

Note that this should say "function foo has finished. bar is 1234".

...if I add in set -x at the start of the script, the curl command (at the end of the script) ends up looking like this:

curl -X GET -G (URL to server) -d bar= -d temperature=21.5-d humidity=65

Again, note that the bar variable is empty.

If I have the same script without the wait command (and run the function without the ampersand after it), the function correctly sets the bar variable, and the resulting curl command is complete. However, when I use the wait command as above, the variables from the foo function are lost once the function has finished.

Is there some way of retaining these variables that are set by the function foo?


Solution

  • Just summarising Barmar's comment here so it can be marked as an answer...

    The issue is that the function running in the background is run in a sub-shell, rather than the same shell that the main script is running in, and the variables that are set in the sub-shell are never transferred into the main shell (in which the script itself is running).

    A possible solution is to write the variables to a file and - after the wait command - read the variables from those files again.