Nth word in a string variable

前端 未结 6 1507
夕颜
夕颜 2020-12-13 08:05

In Bash, I want to get the Nth word of a string hold by a variable.

For instance:

STRING=\"one two three four\"
N=3

Result:

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 08:41

    An alternative

    N=3
    STRING="one two three four"
    
    arr=($STRING)
    echo ${arr[N-1]}
    

提交回复
热议问题