1

I can't figure it out. As I read in documentaion, {} doesn't create a subshell. However, looks like that sometimes it does:

  $ unset T; echo "T_bfr=$T"; echo $$; { echo $$; export T=1; }; echo "T_afr=$T"
T_bfr=
4874
4874
T_afr=1

 $ unset T; echo "T_bfr=$T"; echo $$; { echo $$; export T=1 ; }|cat; echo "T_afr=$T"
T_bfr=
4874
4874
T_afr=

What is the difference? Why T is missing in the second case?

2

The second case is different because that pipe runs in a subshell, where T_aft=$T is unset.

  • But why pipe with subshell wipes variable from curly braces? – rush Dec 11 '12 at 11:43
  • Good question. The short answer is that the subshell is unaware of the newly set variable. Long answer - mywiki.wooledge.org/BashGuide/InputAndOutput#Pipes – lurker Dec 11 '12 at 12:11
  • You may wish to expand your answer with the comment you've given. – EightBitTony Dec 11 '12 at 15:14
  • See BashFAQ 24 about pipes, subshells, and shell variables. – jw013 Dec 11 '12 at 15:35

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.