Issue with cat command on a for loop

Good day to all,

I'd like to ask for your advice with regards to this.

Scenario :
I have here a file named TEST.tmp wherein the value inside is below;

"ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT" "NINE"

But when I'm trying to use this in a simple command like;

for TESTING in $(cat TEST.tmp)
do
 FIRSTID=$(echo -e "${TESTING}")
echo -e "$FIRSTID" >> TESTING.txt
done

The output I am getting is this;

"ONE|TWO|FIVE|THREE|FOUR|SIX~SEVEN~EIGHT"
"NINE"

I'm not sure as to why it is printing the NINE value on a new line.

Please help. Thank you in advance.

This is because the for list splits on IFS that defaults to space,tab,NL.
So a space is treated like a NL.
You can explicitly set IFS to only NL

# subshell starts
(
# split on NL only
IFS="
"
for TESTING in $(cat TEST.tmp); do echo "$TESTING"; done
)
# subshell ended, IFS is default again.

But it is more clever to use read that reads lines

while read line
do
  echo "$line"
done < TEST.tmp

And it splits into distinct variables by IFS (space and tabs only; it will never see an NL because of the read )

while read word1 word2
do
  echo "word1=$word1 word2=$word2"
done < TEST.tmp
1 Like
for TESTING in $(<TEST.tmp); do
        FIRSTID=$(echo -e "$TESTING")
        echo -ne "$FIRSTID"
done > TESTING.txt

HI MadeInGermany,

Thank you so much, that worked!

for TESTING in "$(cat TEST.tmp)"
do
FIRSTID=$(echo -e "${TESTING}")
echo -e "$FIRSTID"
done >> TESTING.txt