Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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