Help identifying the first word in a string

Hi all, I'd like to know how to identify the first word in a string (in bash) for e.g.

echo "enter your name"
read name
(user enters 'Joe Bloggs' for e.g.)

echo "hello $name"
(output says "hello Joe")

Thanks for any help

 
echo "enter your name"
read name
name=$(echo $name | cut -d" " -f1)
echo "hello $name"
read name
echo "Hello ${name%% *}"

Spot on guys, thanks very much for your quick responses, much appreciated

@Scrutinizer
I have seen splitting/tokenizing a string using % OR # earlier also in other threads but never really understood how exactly that works.
Could you please explain that command?

Hi, this particular expansion cuts the longest match of space followed by any character off of the end of the string. It is described here with examples: 2.6.2 Parameter Expansion

1 Like