Discussion:
Bash mystery
Cliff Pratt
2011-07-12 07:51:52 UTC
Permalink
'dir1' contains 'even more stuff', 'more stuff', and 'stuff'

prompt# echo $(find dir1 -type f)

dir1/even more stuff dir1/stuff dir1/more stuff

prompt# echo "$(find dir1 -type f)"

dir1/even more stuff

dir1/stuff

dir1/more stuff

Why the difference?

Cheers,

Cliff

_______________________________________________
NZLUG mailing list ***@linux.net.nz
http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug
Bruce Clement
2011-07-12 08:01:49 UTC
Permalink
The output of find is a group of filenames each followed by a linefeed.

When you don't have the quotes, echo receives one parameter for each word
in th eoutput of the find and the linefeeds are discarded as white space.
When it prints its parameters they appear on one line.

With the quotes, echo receives a single parameter with embedded linefeeds.
When it prints this the linefeeds go to stdout.

HTH

Bruce
Post by Cliff Pratt
'dir1' contains 'even more stuff', 'more stuff', and 'stuff'
prompt# echo $(find dir1 -type f)
dir1/even more stuff dir1/stuff dir1/more stuff
prompt# echo "$(find dir1 -type f)"
dir1/even more stuff
dir1/stuff
dir1/more stuff
Why the difference?
Cheers,
Cliff
______________________________**_________________
http://www.linux.net.nz/cgi-**bin/mailman/listinfo/nzlug<http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug>
--
Bruce Clement

Home: http://www.clement.co.nz/
Twitter: http://twitter.com/Bruce_Clement
Directory: http://www.searchme.co.nz/

"Before attempting to create something new, it is vital to have a good
appreciation of everything that already exists in this field." Mikhail
Kalashnikov
_______________________________________________
NZLUG mailing list ***@linux.net.nz
http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug
Martin D Kealey
2011-07-12 08:23:14 UTC
Permalink
Post by Cliff Pratt
'dir1' contains 'even more stuff', 'more stuff', and 'stuff'
prompt# echo $(find dir1 -type f)
dir1/even more stuff dir1/stuff dir1/more stuff
prompt# echo "$(find dir1 -type f)"
dir1/even more stuff
dir1/stuff
dir1/more stuff
Why the difference?
Case 1: Unquoted strings (such as the interpolation of $(find...)) are split
at whitespace (including newlines) into separate "words". Echo then outputs
a single space between each "word".

Case 2: Given a single "word" as an argument, echo will output that "word"
without modification, including any embedded newlines.

The term "word" here means an indexed positional parameter; it usually
doesn't look much like an English word, and it's only called a "word"
because it's separated by whitespace "unless quoted ...yadda yadda yadda..."

Sometimes it is helpful to think of shell's processing of strings as
progressing from the inside outwards.

Hope that helps.

-Martin

_______________________________________________
NZLUG mailing list ***@linux.net.nz
http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug
Cliff Pratt
2011-07-12 08:39:45 UTC
Permalink
Post by Martin D Kealey
Post by Cliff Pratt
'dir1' contains 'even more stuff', 'more stuff', and 'stuff'
prompt# echo $(find dir1 -type f)
dir1/even more stuff dir1/stuff dir1/more stuff
prompt# echo "$(find dir1 -type f)"
dir1/even more stuff
dir1/stuff
dir1/more stuff
Why the difference?
Case 1: Unquoted strings (such as the interpolation of $(find...)) are split
at whitespace (including newlines) into separate "words". Echo then outputs
a single space between each "word".
Case 2: Given a single "word" as an argument, echo will output that "word"
without modification, including any embedded newlines.
The term "word" here means an indexed positional parameter; it usually
doesn't look much like an English word, and it's only called a "word"
because it's separated by whitespace "unless quoted ...yadda yadda yadda..."
Sometimes it is helpful to think of shell's processing of strings as
progressing from the inside outwards.
Hope that helps.
Thanks,

Cliff

_______________________________________________
NZLUG mailing list ***@linux.net.nz
http://www.linux.net.nz/cgi-bin/mailman/listinfo/nzlug

Continue reading on narkive:
Loading...