First of all, I have little experience using Bash and I apologize for my bad English. Maybe it's obvious.
I am trying to understand why Bash drop value of the variable in this oneliner.
echo "Alpha;Beta;Gamma" | IFS=";" read First Second Third; echo $First $Second $Third
no output
But
echo "Alpha;Beta;Gamma" | (IFS=";" read First Second Third; echo $First $Second $Third)
has the right output
Alpha Beta Gamma.
I guess the read command opens a subshell and when it is close the variables lose their values.
If I am right, how to prevent it?
The goal was to separate a CSV like structure into variables.
Thanks in advice!
BR,
b
while read var ; do x=55 ; done < <(echo fred) echo "$x". But I don't understand why. – bas1c Jan 28 '24 at 18:24lastpipeoption -- but it's not set by default). Whether that last command is awhileloop or a simple command likereaddoes not matter. As for why thedone < <(echo fred)version works, it's because that's not a pipe, so thewhileloop isn't forced into a subshell. – Gordon Davisson Jan 28 '24 at 19:02read, but the pipeline. True,readis one to naturally appear in cases like this, but see Gilles' example ofa=0; a=1 | a=2; echo $ain their answer to thewhile readquestion. – ilkkachu Jan 28 '24 at 20:03a | b; canda | (b; c)is that in the second one, the right-hand side of the pipeline contains the whole group in parenthesis. You can do that, but in some cases you might need to put basically the whole remainder of the script within that group, which might be a bit awkward. – ilkkachu Jan 29 '24 at 07:56lastpipeand disable job control, so runshopt -s lastpipe; set +mand then retry your test. – Gordon Davisson Jan 29 '24 at 09:30