← Back to Bash Course | Chapter 10: Text Processing Tools | Lesson 11 of 12

Subshells क्या हैं

एक subshell ऐसा है जैसे किसी friend को आपके desk की एक photocopy पर काम करने देना। वे चाहे जितना भी rearrange करें, आपका असली desk बिल्कुल वैसा ही रहता है जैसा आपने छोड़ा था।

In this page:

  1. Subshells
Syntax
bash
( commands )

Subshells

Commands को parentheses में wrap करना, ( commands ), उन्हें एक separate subshell में run करता है — एक child process जिसकी अपनी variables और working directory की copy होती है जो parent shell को affect नहीं करती।

अंदर किए गए changes, जैसे cd या variable assignments, subshell exit होते ही गायब हो जाते हैं। Subshells side effects को isolate करने के लिए उपयोगी हैं, जैसे script के बाकी हिस्से को affect किए बिना temporarily directory change करना।

Note: (cd some_dir && command) उपयोग करें किसी दूसरी directory में एक command run करने के लिए बिना अपने script की working directory को permanently बदले।

उदाहरण: Subshells

bash
#!/bin/bash
original_dir=$(pwd)

(
    cd /tmp
    echo "Inside subshell: $(pwd)"
)

echo "Back in parent shell: $(pwd)"
echo "Still matches original: $([[ $(pwd) == $original_dir ]] && echo yes || echo no)"
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. ( ... ) के अंदर variables set करना और उन्हें बाहर उम्मीद करना, क्योंकि एक subshell की अपनी variables होती हैं।
  2. एक subshell के अंदर cd उपयोग करना और उम्मीद करना कि यह script की directory बदल देगा।
  3. यह उम्मीद करना कि एक subshell में exit parent script को रोक देगा।
🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

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.