Process Substitution व्यवहार में (diff <(...) <(...))
In this page:
diff <(command1) <(command2)
दो Command Outputs Compare करना
diff <(cmd1) <(cmd2) आपको दो commands के live output को सीधे diff करने देता है, जो manually हर एक को एक temp file में redirect करने, उन्हें diff करने, और बाद में clean up करने से कहीं ज़्यादा convenient है।
उदाहरण: Comparing Two Command Outputs
#!/bin/bash
diff <(printf "a\nb\nc\n") <(printf "a\nb\nc\n") && echo "identical output, no differences"
Login to try C/C++/Java/PHP code in the editor
एक Difference Detect करना
diff का exit status एक उपयोगी signal भी है: 0 मतलब कोई difference नहीं, 1 मतलब differences मिले, जो एक script को comparison result पर directly branch करने देता है।
उदाहरण: Detecting a Difference
#!/bin/bash
if diff <(printf "a\nb\n") <(printf "a\nc\n") > /dev/null; then
echo "no differences"
else
echo "differences were found"
fi
Login to try C/C++/Java/PHP code in the editor
Data के Sorted Versions Compare करना
Process substitution दूसरी pipelines के साथ अच्छी तरह compose होता है; दो data sets के sorted output compare करना यह check करने का एक common real-world उपयोग है कि दो lists order चाहे जो भी हो same elements रखती हैं या नहीं।
उदाहरण: Comparing Sorted Versions of Data
#!/bin/bash
list_a="banana\napple\ncherry"
list_b="cherry\nbanana\napple"
if diff <(printf "$list_a" | sort) <(printf "$list_b" | sort) > /dev/null; then
echo "both lists contain the same elements"
fi
Login to try C/C++/Java/PHP code in the editor
- उन commands के साथ process substitution उपयोग करने की कोशिश करना जिन्हें एक pipe-like descriptor के बजाय एक असली seekable file चाहिए; कुछ tools इसे support नहीं करते।
- यह भूल जाना कि
diffdifferences मिलने पर एक non-zero exit status return करता है, जो expected behavior है, कोई error नहीं। - एक POSIX
shscript में process substitution उपयोग करना और confused होना कि यह क्यों fail होता है; यह एक bash (और कुछ दूसरी shells) का extension है।
diff <(cmd1) <(cmd2)बिना intermediate temp files के दो commands का live output compare करता है।diffinputs identical होने पर 0 और differ होने पर 1 exit करता है, जो scripts directly check कर सकती हैं।- Process substitution जहां भी एक filename expected हो वहां उपयोग किया जा सकता है, सिर्फ
diffके साथ नहीं। - हर
<(...)अपना command एक subshell में run करता है, इसलिए यह calling script को वापस variables leak नहीं कर सकता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling