Need Help on For Loop to pass space separated value as one value

Hi,

I am having a file say list1 with a output like below

jun 12 18:23
may 20 18:23

Now i want to pass the above two values into for loop,I have written a script like this.

#!/bin/bash

a=`cat list1`
for i in $a
do
echo "HI $i"
done

expected output:

HI jun 12 18:23
HI may 20 18:23

But i am getting output like below,

HI jun
HI 12
HI 18:23
HI may
HI 20
HI 18:23

'for' will loop over items based on IFS, which is whitespace by default.

Since you actually appear to want to process the file by line rather than just by field, it's probably easiest to just use a while/read loop. Something like:

while read line
do
   echo "HI ${line}"
done < list1
1 Like

Thanks a lot CarolM....

You can also use sed for example:

sed 's/^/HI /' file