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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)