Bash – Extracting the second word from a string variable

bashstringvariable

I have a string "rtcpOnNbActive true" stored in a variable x. I want to extract "true" as substring and store in a variable. How can I do this?

Best Answer

Try this way:

y=$(echo $x | awk '{print $2}')
echo $y
  • echo $x display the value of x.
  • awk '{print $2}' prints the second field of the previously displayed x.
  • $(...) hold the output and let assign it to y.
Related Question