Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

People also ask

How do I print the first word in a shell script?

To print a whole word, you want -f 1 , not -c 1 . And since the default field delimiter is TAB rather than SPACE, you need to use the -d option.

How do I find the first word in Linux?

In case your string contains newlines | head -n1 will select the first line first before the important commands select the first word from the string passed to it. The would return the first word of every line, not the first word.

How do I get the first letter of a string in bash?

To access the first character of a string, we can use the (substring) parameter expansion syntax ${str:position:length} in the Bash shell. position: The starting position of a string extraction.

How do you get extract a word from a line in bash shell script?

If "name=foo" is always in the same position in the line you could simply use: awk '{print($3)}' , that will extract the third word in your line which is name=foo in your case. He told "also other parameters may or may not be there" hence you may not use a positional logic.


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.


There isn't any need to use external commands. Bash itself can do the job. Assuming "word1 word2" you got from somewhere and stored in a variable, for example,

$ string="word1 word2"
$ set -- $string
$ echo $1
word1
$ echo $2
word2

Now you can assign $1, $2, etc. to another variable if you like.


I think one efficient way is the use of Bash arrays:

array=( $string ) # Do not use quotes in order to allow word expansion
echo ${array[0]}  # You can retrieve any word. Index runs from 0 to length-1

Also, you can directly read arrays in a pipe-line:

echo "word1 word2" | while read -a array; do echo "${array[0]}" ; done

echo "word1 word2 word3" | { read first rest ; echo $first ; }

This has the advantage that is not using external commands and leaves the $1, $2, etc. variables intact.


Using shell parameter expansion %% *

Here is another solution using shell parameter expansion. It takes care of multiple spaces after the first word. Handling spaces in front of the first word requires one additional expansion.

string='word1    word2'
echo ${string%% *}
word1

string='word1    word2      '
echo ${string%% *}
word1

Explanation

The %% signifies deleting the longest possible match of  * (a space followed by any number of whatever other characters) in the trailing part of string.


If you are sure there are no leading spaces, you can use Bash parameter substitution:

$ string="word1  word2"
$ echo ${string/%\ */}
word1

Watch out for escaping the single space. See here for more examples of substitution patterns. If you have Bash > 3.0, you could also use regular expression matching to cope with leading spaces - see here:

$ string="  word1   word2"
$ [[ ${string} =~ \ *([^\ ]*) ]]
$ echo ${BASH_REMATCH[1]}
word1

You could try AWK:

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

With AWK it is really easy to pick any word you like ($1, $2, etc.).