How to modify a global variable within a function in bash?

后端 未结 8 2145
情深已故
情深已故 2020-12-02 09:20

I\'m working with this:

GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

I have a script like below:

#!/bin/bas         


        
8条回答
  •  旧时难觅i
    2020-12-02 09:30

    It's because command substitution is performed in a subshell, so while the subshell inherits the variables, changes to them are lost when the subshell ends.

    Reference:

    Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment

提交回复
热议问题