← Back to Bash Course | Chapter 11: Process Management | Lesson 8 of 12

Subshells vs Current Shell की तुलना ( ( ) vs { } )

Parentheses एक block of commands को shell की एक separate copy में run करते हैं, जबकि curly braces उन्हें directly current shell में run करते हैं, और यही difference यह decide करता है कि variable changes बाद में टिके रहते हैं या नहीं।
Syntax
bash
( 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

bash
#!/bin/bash
count=1
( count=99; echo "inside subshell: $count" )
echo "outside subshell: $count"

Curly Braces Share the Current Shell

{ commands; } commands को group करता है लेकिन उन्हें current shell environment में run करता है, इसलिए अंदर किए गए variable assignments और cd calls block खत्म होने के बाद भी persist करते हैं।

Note: { के बाद required space और } से पहले semicolon पर ध्यान दें — ये syntax requirements हैं, style choices नहीं।

उदाहरण: Curly Braces Share the Current Shell

bash
#!/bin/bash
count=1
{ count=99; echo "inside group: $count"; }
echo "outside group: $count"

Isolating a Directory Change

एक subshell temporarily किसी जगह cd करने और वहां एक command run करने का एक clean तरीका है बिना बाद में वापस cd करना याद रखे; subshell exit होते ही, parent की working directory unaffected रहती है।

उदाहरण: Isolating a Directory Change

bash
#!/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

exit Behaves Differently in Each

( ... ) के अंदर exit call करना सिर्फ उस subshell को terminate करता है, parent script को continue होने देते हुए; { ...; } के अंदर exit call करना पूरे script को terminate करता है क्योंकि यह उसी process में run होता है।

उदाहरण: exit Behaves Differently in Each

bash
#!/bin/bash
( echo "in subshell before exit"; exit 1; echo "never printed" )
echo "script continues after the subshell exited, status was $?"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि ( ... ) और { ...; } सिर्फ stylistic alternatives हैं; उनका fundamentally different scoping behavior होता है।
  2. यह भूल जाना कि { ...; } को { के बाद एक space, } से पहले एक semicolon (या newline) चाहिए, जबकि ( ... ) को इनमें से कुछ नहीं चाहिए।
  3. किसी ऐसी चीज़ के लिए एक 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 को खत्म करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.