BASH: Variables in a function piped to dialog are lost

59 views Asked by At

I have a script with a function which does some stuff and sets some variables to be used later in the script. This function is also echoing progress (echo 10, echo 20 etc.) and is piped to a "dialog --gauge" to display a progress bar. After the function completes and dialog exits, all variables set in the function are lost.

For simplification, it is the following logic:

myScript.sh:

#!/bin/bash

myFunction() {
    _var1=1
    _var2=2
    _var3=3
    #Do stuff..
    echo 0
    sleep 1
    echo 25
    sleep 1
    echo 50
    sleep 1
    echo 75
    sleep 1
    echo 100
    sleep 1
}

myFunction | dialog --backtitle "Backtitle" --title "Title" --gauge "\\nWait while stuff is being done...\\n\\nProgress:" 10 60

# Or
# dialog --backtitle "Backtitle" --title "Title" --gauge "\\nWait while stuff is being done...\\n\\nProgress:" 10 60 < <(myFunction)

echo -e "Var 1: $_var1" # Is empty
echo -e "Var 2: $_var2" # Is empty
echo -e "Var 3: $_var3" # Is empty

exit 0;

Any ideas on how to implement this without losing the variables?

Thank you!

./myScript.sh
Var 1: 
Var 2: 
Var 3:

EDIT: Thanks to @chepner, the solution proposed in the first comment worked: myFunction > >(dialog --backtitle "Backtitle" --title "Title" --gauge "\\nWait while stuff is being done...\\n\\nProgress:" 10 60)

0

There are 0 answers