linux – Define multiple variables on one line

Question:

I would like to make a parser with just one line and break a string into several variables. In the string I'm going to parse, the default is that each field is separated by a comma.

On several lines, the code to do what I want would look like this:

hostname=`echo $tripa | cut -d, -f1`
ip=`echo $tripa | cut -d, -f2`
serial=`echo $tripa | cut -d, -f3`

Is there a way to do this in just one line (without repeating the cut command several times, once for each variable)?

Answer:

I'm not sure it will work for you.

tripa="meu host,127.0.0.1,abc"
IFS=',' read hostname ip serial <<<"$tripa"
echo -e "$hostname\n$ip\n$serial"

I found this answer here

Scroll to Top