← Back to Bash Course | Chapter 8: Input/Output & Redirection | Lesson 8 of 13

Pipes और Commands को Chain करना

एक pipe एक command के output को सीधे अगले के input से connect करता है, आपको tools की एक छोटी assembly line बनाने देते हुए।
Syntax
bash
command1 | command2
command1 && command2
command1 || command2

Connecting Two Commands

Pipe operator | अपने left वाले command का stdout लेता है और इसे right वाले command के stdin के रूप में feed करता है। आप इस तरह जितने चाहें commands chain कर सकते हैं।

उदाहरण: Connecting Two Commands

bash
#!/bin/bash
printf "banana\napple\ncherry\n" | sort

Pipelines and Exit Status

Default रूप से, एक pipeline का exit status इसके आखिरी command का exit status होता है, चाहे पहले वाले commands fail हुए हों या नहीं। pipefail enable करना इसे बदल देता है इसलिए pipeline fail होता है अगर कोई भी stage fail हो।

Warning: pipefail के बिना, एक लंबी pipeline में एक broken पहली stage silently ignore हो सकती है।

उदाहरण: Pipelines and Exit Status

bash
#!/bin/bash
false | true
echo "default pipeline status: $?"

set -o pipefail
false | true
echo "pipefail status: $?"
set +o pipefail

Chaining with && and ||

&& अगला command सिर्फ तब चलाता है जब पिछला succeed हुआ हो (exit status 0); || अगला command सिर्फ तब चलाता है जब पिछला fail हुआ हो (non-zero exit status)। ये lightweight conditional logic के लिए लगातार उपयोग होते हैं।

उदाहरण: Chaining with && and ||

bash
#!/bin/bash
mkdir -p demo_dir && echo "directory ready"
rmdir demo_dir || echo "could not remove"

false || echo "this runs because the previous command failed"
true && echo "this runs because the previous command succeeded"

Building a Small Pipeline

असली scripts अक्सर एक बड़ा command लिखने के बजाय कई छोटे, single-purpose tools chain करती हैं, जो simple programs compose करने की Unix philosophy को mirror करता है।

उदाहरण: Building a Small Pipeline

bash
#!/bin/bash
printf "3\n1\n2\n1\n3\n1\n" | sort -n | uniq -c
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. यह सोचना कि एक pipeline commands को एक के बाद एक चलाती है; pipeline में हर command असल में एक ही समय पर शुरू होता है और उनके बीच data stream होता है।
  2. एक pipeline के बाद $? check करना और यह मान लेना कि यह पहले command का exit status reflect करता है; default रूप से यह सिर्फ आखिरी command का exit status reflect करता है।
  3. यह भूल जाना कि pipe का हर side अपनी खुद की subshell में चलता है, इसलिए एक piped command के अंदर set किए variables बाद में persist नहीं करते।
चैप्टर सारांश
  • | एक command का stdout सीधे अगले command के stdin से connect करता है।
  • Default रूप से, एक pipeline के बाद $? सिर्फ आखिरी command का exit status reflect करता है।
  • set -o pipefail pipeline का exit status सभी commands में से पहला non-zero status बना देता है।
  • Commands को && (सिर्फ success पर अगला चलाएं) और || (सिर्फ failure पर अगला चलाएं) से chain किया जा सकता है।

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.