I am trying to loop over a variable which contains newlines in a POSIX compatible way.
The following works:
echo "$REQUIRE_PURPOSE" | while IFS= read -r line ; do
echo "$line"
done
but the piping spawns another shell.
I then tried:
OLDIFS="$IFS"
IFS='
'
for line in "$REQUIRE_PURPOSE"; do
echo "$line"
done
IFS="$OLDIFS"
But it just prints the content of "$REQUIRE_PURPOSE"
I also tried:
while IFS= read -r line ; do
echo "$line"
done <<< "$REQUIRE_PURPOSE"
but also here it does not work.
Example:
echo "$REQUIRE_PURPOSE"
echo "---"
while IFS= read -r line ; do echo "$line"; done <<< "$REQUIRE_PURPOSE"
generates:
uuu
iii
Digital Signature
---
uuuniiinDigital Signature
or
00000000 75 75 75 0a 69 69 69 0a 44 69 67 69 74 61 6c 20 |uuu.iii.Digital |
00000010 53 69 67 6e 61 74 75 72 65 0a 2d 2d 2d 0a 31 20 |Signature.---.1 |
00000020 75 75 75 6e 69 69 69 6e 44 69 67 69 74 61 6c 20 |uuuniiinDigital |
00000030 53 69 67 6e 61 74 75 72 65 0a 75 75 75 0a 69 69 |Signature.uuu.ii|
00000040 69 0a 44 69 67 69 74 61 6c 20 53 69 67 6e 61 74 |i.Digital Signat|
00000050 75 72 65 0a |ure.|
00000054
Any idea?
while ... do ... done <<< "$REQUIRE_PURPOSE"works for me. What problem do you have?REQUIRE_PURPOSE? Can you add code that sets the variable? Did you intentionally useread -ainstead ofread -rin the code below "Example:"?\andninstead of real linefeed characters?0a(nl)