Assigning dynamic bash variable names using a for loop seq

后端 未结 3 1055
离开以前
离开以前 2020-12-19 18:56

So I\'m trying to do something, not sure if it\'s possible. I have the following code:

for i in {0..5}; do
    if [[ -f ./user$i ]]; then
        group$i=$(g         


        
3条回答
  •  有刺的猬
    2020-12-19 19:22

    Try to use the export or declare function like this

    for i in {0..5}; do
        if [[ -f ./user$i ]]; then
            export group$i=$(grep -w "group" ......
    

    with declare

    for i in {0..5}; do
        if [[ -f ./user$i ]]; then
            declare group$i=$(grep -w "group" ......
    

    where export makes the value available to sub-processes, and declare just available within the same script.

提交回复
热议问题