Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why echo strings in bash reading from stdin doesn't show space characters

Tags:

bash

shell

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.

like image 858
wukong Avatar asked May 25 '26 03:05

wukong


2 Answers

Several issues with your code.

  1. $parameter outside of " ". Don't.
  2. read uses $IFS to split input line into words, you have to disable this.
  3. UUOC

To summarize:

 while IFS= read line ; do echo "$line" ; done < test.txt > new.txt
like image 107
n. 1.8e9-where's-my-share m. Avatar answered May 26 '26 18:05

n. 1.8e9-where's-my-share m.


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"

like image 45
mogul Avatar answered May 26 '26 16:05

mogul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!