Take this piece of code that reads in data separated by |
DATA1="Andreas|Sweden|27"
DATA2="JohnDoe||30" # <---- UNKNOWN COUNTRY
while IFS="|" read -r NAME COUNTRY AGE; do
echo "NAME: $NAME";
echo "COUNTRY: $COUNTRY";
echo "AGE: $AGE";
done<<<"$DATA2"
OUTPUT:
NAME: JohnDoe
COUNTRY:
AGE: 30
It should work identically to this piece of code, where we are doing the exact same thing, just using \t
as a separator instead of |
DATA1="Andreas Sweden 27"
DATA2="JohnDoe 30" # <---- THERE ARE TWO TABS HERE
while IFS=$'\t' read -r NAME COUNTRY AGE; do
echo "NAME: $NAME";
echo "COUNTRY: $COUNTRY";
echo "AGE: $AGE";
done<<<"$DATA2"
But it doesn't.
OUTPUT:
NAME: JohnDoe
COUNTRY: 30
AGE:
Bash, or read
or IFS
or some other part of the code is globbing together the whitespace when it isn't supposed to. Why is this happening, and how can I fix it?