2
$ cat temp.txt
hello, world!





$ cat temp.txt | sed -n '1,4p'
hello, world!



$ ret=$(cat temp.txt | sed -n '1,4p')
$ echo "$ret"
hello, world!
$

I am wondering why the $ret variable does not have the empty lines.

Much appreciated for the help.

1

2 Answers 2

4

From the bash man page (emphasis mine):

Bash performs the expansion by executing command and replacing the command substitution with > the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting. The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

-1

The parsing of the echo "$ret" skips whitespace in determining what the actual arguments to echo will be. Play around with things like this to understand better:

x=$(echo '   foo    ')
echo "x${x}x"
echo ${x}
echo "${x}"
1
  • He is quoting $ret, though. The question is, why aren't the blank lines in $ret?
    – chepner
    Aug 28, 2012 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.