in shell script I'm trying to read a file line by line using while loop but character 'n' is replaced with blank space in all lines.
here is the output of cat namespaces.tx
cat namespaces.txt
default
kube-node-lease
kube-public
kube-system
name
mane
mann
here is the output it is printing when reading from while loop
while IFS=$'\n' read -r line; do echo $line; done < namespaces.txt
default
kube- ode-lease
kube-public
kube-system
ame
ma e
ma
CodePudding user response:
while IFS= read -r line; do
echo "$line"
done < namespaces.txt
This is the correct way to read and print a line at a time. "$line"
is quoted, and IFS
can be set to empty.
Check for IFS=\n
, or similar, in your script. Somewhere, you have set IFS
to contain n
. Then when you print $line
with no quotes, it gets split in to multiple words by IFS
(n
), and echo prints each word given to it, with a space in between.