Can't access variables inside parenthesis in bash

后端 未结 1 1282
借酒劲吻你
借酒劲吻你 2021-01-27 01:15

In bash, if I run

(foo=14)

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

echo \"${foo}\"
<
相关标签:
1条回答
  • 2021-01-27 01:32

    Commands enclosed in parenthesis e.g. () are executed in a sub-shell. Any assignment in a sub-shell will not exist outside that sub-shell.

    foo=14
    bar=$(echo $foo | tr '1' 'a' )
    if [[ $? -eq 0 ]]
    then
        echo "Setting "'$bar'" was a success. It is ${bar}"
    else
        echo "Setting "'$bar'" failed with a nonzero exit code."
    fi
    
    0 讨论(0)
提交回复
热议问题