← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 8 of 12

Command Substitution का उपयोग

Command substitution आपको एक command run करने देता है और उसका printed output सीधे एक variable या दूसरे command में डालने देता है, जैसे आपने वह output खुद type किया हो।
Syntax
bash
variable_name=$(command)

variable_name=`command`

Basic $(...) Substitution

$(command) command को execute करता है और जो भी वह command standard output पर print करता है उससे replace हो जाता है, किसी भी trailing newline को हटाकर। Result एक variable में store किया जा सकता है या directly दूसरे command में inline उपयोग किया जा सकता है।

उदाहरण: Basic $(...) Substitution

bash
#!/bin/bash
current_dir=$(pwd)
echo "Working directory: $current_dir"
file_count=$(ls | wc -l)
echo "Files here: $file_count"

Legacy Backtick Syntax

Backticks ` command command substitution करने का पुराना तरीका हैं और simple cases के लिए $(...)` जैसे ही behave करते हैं। इन्हें legacy माना जाता है क्योंकि इन्हें nest करने के लिए inner backticks को backslashes से escape करना पड़ता है, जो जल्दी unreadable हो जाता है।

Warning: नई scripts में backticks से ज़्यादा $(...) prefer करें -- यह cleanly nest होता है और visually match करना आसान है।

उदाहरण: Legacy Backtick Syntax

bash
#!/bin/bash
today=`date +%Y-%m-%d`
echo "Today's date is $today"

Command Substitutions को Nest करना

$(...) बिना किसी special escaping के directly किसी और $(...) के अंदर nest हो सकता है, जो backticks पर इसका सबसे बड़ा advantage है। Bash पहले innermost substitution resolve करता है, फिर outer वाला।

उदाहरण: Nesting Command Substitutions

bash
#!/bin/bash
year=$(date +%Y)
decade=$(echo "$(( year / 10 * 10 ))s")
echo "We are in the $decade"

Commands में Directly Substitution उपयोग करना

Command substitution को पहले एक variable में assign करना ज़रूरी नहीं -- इसे किसी भी दूसरे command में inline एक argument के रूप में उपयोग किया जा सकता है, जो आपको commands साथ compose करने देता है।

Substitution को double quotes से quote करना internal spacing preserve करता है और unwanted word splitting रोकता है।

उदाहरण: Using Substitution Directly in Commands

bash
#!/bin/bash
echo "There are $(ls | wc -l) entries in $(pwd)"
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. यह सोचना कि command substitution एक command का exit status capture करता है; यह असल में standard output text capture करता है, और substitution का exit status इसके अंदर आखिरी command का exit status है ($? से अलग access किया जा सकता है)।
  2. एक command substitution को quote करना भूल जाना, जैसे files=$(ls) फिर for f in $files, जो whitespace/newlines पर word splitting trigger करता है -- आमतौर पर आप इसे एक single string मानते समय "$(...)" चाहते हैं।
  3. यह मान लेना कि backticks और $(...) हर case में पूरी तरह interchangeable हैं; backticks को nest और पढ़ना मुश्किल है (inner backticks को backslash-escape करना पड़ता है), इसलिए modern style $(...) prefer करती है।
चैप्टर सारांश
  • $(command) command run करता है और इसके standard output (trailing newlines हटाकर) को जगह पर substitute करता है।
  • Backticks ` command ` वही काम करते हैं लेकिन पुराने, legacy syntax हैं जिन्हें nest करना ज़्यादा मुश्किल है।
  • $(...) cleanly nest होता है, जैसे $(echo $(date +%Y)), जबकि nested backticks को escaping चाहिए।
  • Command substitution सिर्फ stdout capture करता है, stderr नहीं, जब तक आप explicitly पहले stderr को stdout में redirect न करें (2>&1)।

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.