Jonathan Y.
Jonathan Y.

Reputation: 556

Redirection problems

I've noticed something I don't understand while attempting to keep a progress log in a bash script. The issue can be reproduced using the following line:

var1=$(var2=$(echo "Hi!"))

After executing this line, var2 remains empty. I don't understand what causes this behavior (if the output generated by echo is redirected somewhere, it doesn't go into var1), and more importantly, what I might do to avoid having this problem.

Any sort of help (explanation/pointers) would be much appreciated.

Upvotes: 1

Views: 54

Answers (1)

anubhava
anubhava

Reputation: 786319

The reason why original string remains unchanged because of your use of:

 $(var2=$(echo "Hi!"))

Which basically changes value of var2 in a sub shell and any changes made there remain in sub shell only. Once sub shell exits you don't get changed value in the parent shell.

Upvotes: 5

Related Questions