grep output into array

后端 未结 5 1321
清酒与你
清酒与你 2021-02-05 23:29

Guys How can I make this work

`find /xyz/abc/music/ |grep def`

I don\'t want to store the array in any temporary variable. How can w

5条回答
  •  北海茫月
    2021-02-06 00:13

    Even though a bit late, the best solution should be the answer from Ray, but you'd have to overwrite the default field separator environment variable IFS to newline for taking complete lines as an array field. After filling your array, you should switch IFS back to the original value. I'll expand Rays solution:

    
    
        # keep original IFS Setting
        IFS_BAK=${IFS}
        # note the line break between the two quotes, do not add any whitespace, 
        # just press enter and close the quotes (escape sequence "\n" for newline won't do)
        IFS="
        "
        X=( $(find /xyz/abc/music/ | grep def) )
        echo ${X[1]}
        echo ${X[2]}
        echo ${X[3]}
        echo ${X[4]}
        # set IFS back to normal..
        IFS=${IFS_BAK}
    
    
    

    Hope this helps

提交回复
热议问题