oyyko
oyyko

Reputation: 49

Different results when running in $() or not in it

This is one of the weirdest things I've come across.

➜  ~ cat word.txt
mere doctrine
➜  ~ tr ' ' '\n' < ~/word.txt
mere
doctrine
➜  ~ echo $(tr ' ' '\n' < ~/word.txt)
mere doctrine

I don't understand what is the difference between executing inside $() environment and executing directly.

I am using zsh on manjaro linux.

Upvotes: 1

Views: 48

Answers (1)

Vadik Sirekanyan
Vadik Sirekanyan

Reputation: 4622

When you write, for example, echo $(ls), word splitting occurs.

The best practice is to add double quotes to prevent word splitting:

➜  ~ echo "$(tr ' ' '\n' < word.txt)"
mere
doctrine

Upvotes: 1

Related Questions