How can I retrieve the first word of the output of a command in Bash?

bash

Solution

AWK is a good option if you have to deal with trailing whitespace because it'll take care of it for you:

echo "   word1  word2 " | awk '{print $1;}' # Prints "word1"

cut won't take care of this though:

echo "  word1  word2 " | cut -f 1 -d " " # Prints nothing/whitespace

'cut' here prints nothing/whitespace, because the first thing before a space was another space.

Problem

I have a command, for example: `echo "word1 word2"`. I want to put a pipe (`|`) and get "word1" from the command. ``` echo "word1 word2" | .... ``` What should I put after the pipe?

Original source