How to split one string into multiple strings separated by at least one space in bash shell?

前端 未结 8 1315
日久生厌
日久生厌 2020-11-27 09:17

I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?

The stri

8条回答
  •  日久生厌
    2020-11-27 09:59

    Did you try just passing the string variable to a for loop? Bash, for one, will split on whitespace automatically.

    sentence="This is   a sentence."
    for word in $sentence
    do
        echo $word
    done
    

     

    This
    is
    a
    sentence.
    

提交回复
热议问题