Nth word in a string variable

前端 未结 6 1497
夕颜
夕颜 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条回答
  •  一向
    一向 (楼主)
    2020-12-13 08:25

    No expensive forks, no pipes, no bashisms:

    $ set -- $STRING
    $ eval echo \${$N}
    three
    

    But beware of globbing.

提交回复
热议问题