jeudi 8 août 2019

Can't access variables inside parenthesis in bash

In bash, if I run

(foo=14)

And then try to reference that variable later on in my bash script:

echo "${foo}"

I don't get anything. How can I make bash store this variable the way I need it to?

Specifically, I am using this in an if statement and checking the exit code, something kind of like:

if (bar="$(foo=14;echo "${foo}"|tr '1' 'a' 2>&1)")
then
    echo "Setting "'$bar'" was a success. It is ${bar}"
else
    echo "Setting "'$bar'" failed with a nonzero exit code."
fi

Aucun commentaire:

Enregistrer un commentaire