Subshells क्या हैं
एक subshell ऐसा है जैसे किसी friend को आपके desk की एक photocopy पर काम करने देना। वे चाहे जितना भी rearrange करें, आपका असली desk बिल्कुल वैसा ही रहता है जैसा आपने छोड़ा था।
In this page:
Syntax
( 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
#!/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)"
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
( ... )के अंदर variables set करना और उन्हें बाहर उम्मीद करना, क्योंकि एक subshell की अपनी variables होती हैं।- एक subshell के अंदर
cdउपयोग करना और उम्मीद करना कि यह script की directory बदल देगा। - यह उम्मीद करना कि एक subshell में
exitparent script को रोक देगा।
🔒
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: