Command Substitution का उपयोग
In this page:
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
#!/bin/bash
current_dir=$(pwd)
echo "Working directory: $current_dir"
file_count=$(ls | wc -l)
echo "Files here: $file_count"
Login to try C/C++/Java/PHP code in the editor
Legacy Backtick Syntax
Backticks ` command command substitution करने का पुराना तरीका हैं और simple cases के लिए $(...)` जैसे ही behave करते हैं। इन्हें legacy माना जाता है क्योंकि इन्हें nest करने के लिए inner backticks को backslashes से escape करना पड़ता है, जो जल्दी unreadable हो जाता है।
$(...) prefer करें -- यह cleanly nest होता है और visually match करना आसान है।उदाहरण: Legacy Backtick Syntax
#!/bin/bash
today=`date +%Y-%m-%d`
echo "Today's date is $today"
Login to try C/C++/Java/PHP code in the editor
Command Substitutions को Nest करना
$(...) बिना किसी special escaping के directly किसी और $(...) के अंदर nest हो सकता है, जो backticks पर इसका सबसे बड़ा advantage है। Bash पहले innermost substitution resolve करता है, फिर outer वाला।
उदाहरण: Nesting Command Substitutions
#!/bin/bash
year=$(date +%Y)
decade=$(echo "$(( year / 10 * 10 ))s")
echo "We are in the $decade"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
echo "There are $(ls | wc -l) entries in $(pwd)"
Login to try C/C++/Java/PHP code in the editor
- यह सोचना कि command substitution एक command का exit status capture करता है; यह असल में standard output text capture करता है, और substitution का exit status इसके अंदर आखिरी command का exit status है (
$?से अलग access किया जा सकता है)। - एक command substitution को quote करना भूल जाना, जैसे
files=$(ls)फिरfor f in $files, जो whitespace/newlines पर word splitting trigger करता है -- आमतौर पर आप इसे एक single string मानते समय"$(...)"चाहते हैं। - यह मान लेना कि backticks और
$(...)हर case में पूरी तरह interchangeable हैं; backticks को nest और पढ़ना मुश्किल है (inner backticks को backslash-escape करना पड़ता है), इसलिए modern style$(...)prefer करती है।
$(command)commandrun करता है और इसके 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)।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: