Subshells vs Current Shell की तुलना ( ( ) vs { } )
In this page:
( commands ) # runs in a subshell
{ commands; } # runs in the current shell
Variable Changes Inside a Subshell Don't Escape
चूंकि ( ... ) shell की एक separate copy fork करता है, इसके अंदर किए गए कोई भी variable assignments subshell खत्म होते ही खो जाते हैं; parent shell की variables untouched रहती हैं।
उदाहरण: Variable Changes Inside a Subshell Don't Escape
#!/bin/bash
count=1
( count=99; echo "inside subshell: $count" )
echo "outside subshell: $count"
Login to try C/C++/Java/PHP code in the editor
Curly Braces Share the Current Shell
{ commands; } commands को group करता है लेकिन उन्हें current shell environment में run करता है, इसलिए अंदर किए गए variable assignments और cd calls block खत्म होने के बाद भी persist करते हैं।
{ के बाद required space और } से पहले semicolon पर ध्यान दें — ये syntax requirements हैं, style choices नहीं।उदाहरण: Curly Braces Share the Current Shell
#!/bin/bash
count=1
{ count=99; echo "inside group: $count"; }
echo "outside group: $count"
Login to try C/C++/Java/PHP code in the editor
Isolating a Directory Change
एक subshell temporarily किसी जगह cd करने और वहां एक command run करने का एक clean तरीका है बिना बाद में वापस cd करना याद रखे; subshell exit होते ही, parent की working directory unaffected रहती है।
उदाहरण: Isolating a Directory Change
#!/bin/bash
mkdir -p sub_demo
echo "before: $(pwd | xargs basename)"
( cd sub_demo && echo "inside subshell: $(pwd | xargs basename)" )
echo "after: $(pwd | xargs basename)"
rmdir sub_demo
Login to try C/C++/Java/PHP code in the editor
exit Behaves Differently in Each
( ... ) के अंदर exit call करना सिर्फ उस subshell को terminate करता है, parent script को continue होने देते हुए; { ...; } के अंदर exit call करना पूरे script को terminate करता है क्योंकि यह उसी process में run होता है।
उदाहरण: exit Behaves Differently in Each
#!/bin/bash
( echo "in subshell before exit"; exit 1; echo "never printed" )
echo "script continues after the subshell exited, status was $?"
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
( ... )और{ ...; }सिर्फ stylistic alternatives हैं; उनका fundamentally different scoping behavior होता है। - यह भूल जाना कि
{ ...; }को{के बाद एक space,}से पहले एक semicolon (या newline) चाहिए, जबकि( ... )को इनमें से कुछ नहीं चाहिए। - किसी ऐसी चीज़ के लिए एक subshell उपयोग करना जिसे calling script की state को affect करना ही चाहिए, जैसे
cdया एक variable export करना, और यह देखकर confused होना कि change गायब हो जाता है।
( commands )एक subshell में run होता है: shell की एक forked copy जिसकी अपनी variables, working directory, और traps होती हैं।{ commands; }current shell में run होता है: variable changes,cd, औरexitसभी calling shell को directly affect करते हैं।- एक subshell side effects isolate करने के लिए उपयोगी है, जैसे script के बाकी हिस्से को affect किए बिना temporarily directory बदलना।
( ... )के अंदरexitसिर्फ subshell को खत्म करता है, पूरे script को नहीं;{ ...; }के अंदरexitपूरे script को खत्म करता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: