9

When I do ls | grep png the output of grep is:

2015-05-15-200203_1920x1080_scrot.png 2015-05-16-025536_1920x1080_scrot.png 

(filename,newline,filename,newline)

then, echo $(ls | grep png) outputs:

2015-05-15-200203_1920x1080_scrot.png 2015-05-16-025536_1920x1080_scrot.png 

(filename,space from word splitting,filename,newline !!from echo!!)

That is all ok, but when I do this to prevent the word splitting: echo "$(ls | grep png)", the output is:

2015-05-15-200203_1920x1080_scrot.png 2015-05-16-025536_1920x1080_scrot.png 

And my question is, where is the second newline (one should be from grep and one from echo)?

1 Answer 1

8

That's newline from echo, you can verify by using echo -n to suppress trailing newline:

echo -n "$(ls | grep png)" 

Command substitution remove all trailing newlines, last newline was added by echo, grep has nothing to do here.

1
  • "Command substitution remove all trailing newlines" - thats it! Thank you Commented May 17, 2015 at 16:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.