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

stdin, stdout, stderr और File Descriptors

हर running program के पास text के लिए तीन default pipes होते हैं: एक input पढ़ने के लिए, और दो output भेजने के लिए, और Bash इन्हें 0, 1, और 2 numbers से बुलाता है।
Syntax
bash
command 1> file      # stdout (fd 1)
command 2> file      # stderr (fd 2)
command < file       # stdin (fd 0)
echo "message" >&2

The Three Standard Streams

हर process तीन open file descriptors के साथ शुरू होता है: 0 (stdin, input), 1 (stdout, normal output), और 2 (stderr, error output)। Bash आपको N> syntax उपयोग करके इनमें से किसी को भी explicitly target करने देता है।

उदाहरण: The Three Standard Streams

bash
#!/bin/bash
echo "this goes to stdout" 1>&1
echo "this goes to stderr" 1>&2

Sending Error Messages to stderr

अच्छी तरह से behave करने वाली scripts diagnostics और errors को >&2 के साथ stderr पर print करती हैं ताकि stdout पर normal output clean रहे और separately pipe या capture किया जा सके।

Note: हर जगह >&2 फैलाने के बजाय इस छोटे log_error helper जैसा उपयोग करें।

उदाहरण: Sending Error Messages to stderr

bash
#!/bin/bash
log_error() {
    echo "ERROR: $1" >&2
}

log_error "something went wrong"
echo "normal output continues on stdout"

Order Matters When Merging Streams

cmd > file 2>&1 पहले stdout को file पर point करता है, फिर stderr को (अब redirected) stdout की एक copy बनाता है, इसलिए दोनों file में जाते हैं।

Order को cmd 2>&1 > file में reverse करना stderr को stdout के *old* (terminal) की एक copy बनाता है stdout file में जाने से पहले।

Warning: Redirections को left से right पढ़ें; 2>&1 > file एक बहुत common bug है क्योंकि यह वह नहीं करता जो लोग expect करते हैं।

उदाहरण: Order Matters When Merging Streams

bash
#!/bin/bash
{ echo "out line"; echo "err line" >&2; } > both.log 2>&1
echo "--- both.log contents ---"
cat both.log
rm -f both.log

Checking Which Stream a Descriptor Points To

test -t operator (या [ -t N ]) आपको बताता है कि file descriptor N एक terminal से connected है या नहीं। एक non-interactive script में यह usually नहीं होता, जो decide करने के लिए useful है कि colored output print करना है या नहीं।

उदाहरण: Checking Which Stream a Descriptor Points To

bash
#!/bin/bash
if [ -t 1 ]; then
    echo "stdout is a terminal"
else
    echo "stdout is redirected or non-interactive"
fi
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. यह सोचना कि echo से print किए error messages automatically stderr पर जाते हैं; plain echo हमेशा stdout (fd 1) पर लिखता है जब तक redirect न किया जाए।
  2. 2>&1 placement के order को confuse करना; cmd > file 2>&1 और cmd 2>&1 > file अलग behave करते हैं क्योंकि redirections left से right apply होते हैं।
  3. यह मान लेना कि file descriptor numbers एक script में हमेशा के लिए fixed हैं; वे किसी भी point पर closed, duplicated, और reopened हो सकते हैं।
चैप्टर सारांश
  • File descriptor 0 stdin है, 1 stdout है, 2 stderr है।
  • echo और printf default रूप से stdout पर लिखते हैं; सिर्फ explicit redirection text को stderr पर भेजता है।
  • Command के बाद >&2 उस command के stdout को stderr में redirect करता है।
  • Redirections left से right process होते हैं, इसलिए इन्हें combine करते समय order मायने रखता है।

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.