Bash, weird variable scope when populating array with results

后端 未结 1 1875
一个人的身影
一个人的身影 2020-12-31 02:26

I am parsing command output and putting results into array.

Works fine until exiting the inner loop - output array is empty.

declare -a KEYS

#------         


        
相关标签:
1条回答
  • 2020-12-31 02:41

    When you use a pipe (|) to pass your command output to while, your while loop runs in a subshell. When the loop ends, your subshell terminates and your variables will not be available outside your loop.

    Use process substitution instead:

    while read line; do
       # ...
    done < <($glue_dir/get_keys $ip)
    
    0 讨论(0)
提交回复
热议问题