Here is my code
michal@argon:~$ cat test.txt
1
2
3
4
5
michal@argon:~$ cat test.txt | while read line;do echo $line;done > new.txt
michal@argon:~$ cat new.txt
1
2
3
4
5
I don't know why the command echo $line filtered the space character, I want test.txt and new.txt to be exactly the same.
Please help, thanks.
Several issues with your code.
$parameter outside of " ". Don't.read uses $IFS to split input line into words, you have to disable this.To summarize:
while IFS= read line ; do echo "$line" ; done < test.txt > new.txt
While the provided answers solves your task at hand, they do not explain why bash and echo "forgot" to print the spaces you have in your string. Lets first make an small example to show the problem. I simply run the commands in my shell, no real script needed for this one:
mogul@linuxine:~$ echo something
something
mogul@linuxine:~$ echo something
something
Two echo commands that both print something right at the beginning of the line, even if the first one had plenty space between echo and something. And now with quoting:
mogul@linuxine:~$ echo " something"
something
Notice, here echo printed the leading spaces before something
If we stuff the string into a variable it work exactly the same:
mogul@linuxine:~$ str=" something"
mogul@linuxine:~$ echo $str
something
mogul@linuxine:~$ echo "$str"
something
Why?
Because the shell, bash in your case, removes space between arguments to commands before passing them on to the sub process. By quoting the strings we tell bash that we mean this literally and it should not mess with out strings.
This knowledge will become quite valuable if you are going to handle files with funny names, like "this is a file with blanks in its name.txt"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With