Why is bash only appending first element to array

后端 未结 1 1401
失恋的感觉
失恋的感觉 2020-12-11 17:45

I\'m trying to read a list of files from stdin, with each file delimited by a newline, however I\'m noticing that only the first element is getting appended to the list. I n

相关标签:
1条回答
  • 2020-12-11 18:25

    Actually your files+=( "$input" ) expression is adding elements to your array but you are not iterating it correctly.

    Your last loop should be:

    for f in "${files[@]}"; do
        echo "element is: $f"
    done
    

    Test (thanks to @fedorqui)

    $ a+=(1)
    $ a+=("hello")
    $ a+=(3)
    $ for i in "${a[@]}"; do echo "$i"; done
    1
    hello
    3
    
    0 讨论(0)
提交回复
热议问题