for loop in Unix

This is the content of a file work.log
1 TGBUS I-US 0;15;83

i did this
for i in `cat work.log`
do
echo $i
done

I wanted the out put to be
1 TGBUS I-US 0;15;83

But the output appears as
1
TGBUS
I-US
0;15;83

For Loop treats space as a delimiter. Can i overrride this space as delimiter
Can anyone assist

Try this to get rid of the spaces:

while read i
do
  echo "$i"
done < work.log

This works. But theres no way to do the same with a for loop.

Just out of curiosity.

for i in "`cat work.log`"
do
echo "$i\n"
done

If i have undertood what you want, i think that:

for i in `cat work.log`
do
echo "$i \c"
done

it could be.

\c : suppress trailing newline
or
echo -n : do not output the trailing newline

I should avoid the use of cat, but if you really want to use the for loop, you can change the IFS temporary like this:

#!/bin/sh

OLDIFS="$IFS" 

IFS=""

for i in `cat work.log`;do 
  echo "$i"
done

IFS="$OLDIFS"

But ..... doesn't he want the output in a unique line ????

No pogdorica. He wants to override the space like a delimiter but keep the new line as delimiter

The while loop is much better. The for loop you've done is a bad programming habit sometimes called useless use of backticks -- a waste of memory, processes, and a weak link in your program. Every byte in that file gets read and written an extra time by a completely useless cat, then crammed all at once into one enormous shell variable before it can even start to use the data. Iif it'll fit, that is -- there are limits to the size of a shell variable, system-dependent, which can be quite small. If it doesn't fit, it might truncate it, or refuse to work at all with an error message about too many arguments.

The while loop reads once instead of twice, writes once instead of twice, doesn't need to store the entire file in memory, and will work with any size of file.

If there is only one line in the file "for" or "while" are irrelevant.

cat work.log|read i
echo "${i}"

1 TGBUS I-US 0;15;83

Ps. If there is more than one line, "while" is preferable to "for" as advised above.

Not only UUOC, but that will not work in most shells as the read command is executed in a subshell and $i will not be available anywhere else in the script.

read i < work.log