Nth word in a string variable

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:

"three"

What Bash command/script could do this?


Solution 1:

echo $STRING | cut -d " " -f $N

Solution 2:

An alternative

N=3
STRING="one two three four"

arr=($STRING)
echo ${arr[N-1]}

Solution 3:

Using awk

echo $STRING | awk -v N=$N '{print $N}'

Test

% N=3
% STRING="one two three four"
% echo $STRING | awk -v N=$N '{print $N}'
three