Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take a string and split it into 2 variables in bash?

I have a string that is read in from a file in the format "firstname lastname". I want to split that string and put it into two separate variables $first and $last. What is the easiest way to do this?

like image 817
waa1990 Avatar asked Dec 13 '22 07:12

waa1990


1 Answers

read can do the splitting itself, e.g.

read  first last < name.txt

echo "$first"
echo "$last"
like image 169
Fritz G. Mehner Avatar answered Mar 16 '23 19:03

Fritz G. Mehner