5

In zsh, the following 2 commands results differently:

a=$(</etc/hosts) && echo $a
echo $(</etc/hosts)

The 1st prints contents line by line, while the 2nd prints contents as a whole in one single line.

I guess it is because the subshell returns result line by line to the main echo process, but I can not confirm that.

Someone can help me to clear that?

4

In zsh, unquoted variables are not automatically split and glob, while unquoted command substitution will be split into words using values in IFS.

In your case, the first command saved content of file /etc/hosts to variable a, echo $a prints value of a variable without split and glob, you get the contents of /etc/hosts (split and glob are not performed in RHS of variable assignment).

echo $(</etc/hosts) used unquoted command substitution, the result will be split into words, so you get content of /etc/hosts as all words delimited by space.

  • Thanks a lot! So in the unquoted command substitution, the newlines are replaced by spaces. – carl May 2 '15 at 17:39
  • 1
    Yes, because newline is also in IFS by default. You can read more here – cuonglm May 2 '15 at 17:52
  • Got it, and I'm reading it. Thanks for helping me on my first SE question :D – carl May 2 '15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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