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

Redirection Operators का उपयोग

Redirection operators एक script को इसका output screen के बजाय एक file में भेजने देते हैं, या keyboard के बजाय एक file से input खींचने देते हैं।
Syntax
bash
command > file      # overwrite
command >> file     # append
command < file      # read input from file

Writing and Overwriting with >

> operator एक command का stdout एक file में भेजता है, ज़रूरत पड़ने पर इसे बनाते हुए और अगर यह पहले से मौजूद हो तो इसे पूरी तरह truncate करते हुए। एक जैसे > command का हर run पुराना content मिटा देता है।

Warning: > मौजूदा file content को बिना किसी confirmation के तुरंत destroy करता है।

उदाहरण: Writing and Overwriting with >

bash
#!/bin/bash
echo "first line" > out.txt
echo "second run overwrites" > out.txt
cat out.txt
rm -f out.txt

Appending with >>

>> operator output को एक file के आखिर में append करता है इसे truncate करने के बजाय, अगर यह पहले से मौजूद न हो तो इसे बनाते हुए। यह उन log files के लिए safe choice है जिन्हें समय के साथ accumulate होना चाहिए।

उदाहरण: Appending with >>

bash
#!/bin/bash
echo "line 1" > log.txt
echo "line 2" >> log.txt
echo "line 3" >> log.txt
cat log.txt
rm -f log.txt

Reading Input from a File with <

< operator एक file की contents को एक command के stdin में redirect करता है, जैसे file का text type किया गया हो। यह किसी भी command के साथ काम करता है जो stdin से पढ़ता है, read और cat शामिल।

उदाहरण: Reading Input from a File with <

bash
#!/bin/bash
echo "data from a file" > input.txt
read -r line < input.txt
echo "Read: $line"
rm -f input.txt

Redirecting stderr and Combining Streams

2> सिर्फ stderr को एक file में भेजता है, stdout untouched छोड़ते हुए। &> (या > file 2>&1) दोनों streams को उसी destination पर भेजता है, जो एक command जो भी produce करे उसे capture करने के लिए convenient है।

Note:
  • &> एक bash extension है
  • POSIX sh scripts को इसके बजाय > file 2>&1 उपयोग करना चाहिए।

उदाहरण: Redirecting stderr and Combining Streams

bash
#!/bin/bash
ls not_a_real_file 2> errors.txt
echo "stderr captured:"
cat errors.txt
rm -f errors.txt

{ echo out; echo err >&2; } &> combined.txt
echo "combined captured:"
cat combined.txt
rm -f combined.txt
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. >> मतलब होने पर > उपयोग करना और accidentally एक file का पुराना content erase कर देना।
  2. यह भूल जाना कि 2> सिर्फ stderr redirect करता है, इसलिए stdout अब भी screen पर print होता है जब तक इसे भी redirect न किया जाए।
  3. यह believe करना कि < file किसी तरह यह बदल देता है कि एक command क्या output करता है; यह सिर्फ यह बदलता है कि command अपना input कहां से पढ़ता है।
चैप्टर सारांश
  • > truncate करता है और लिखता है; >> मौजूदा content erase किए बिना append करता है।
  • < एक file की contents को एक command के stdin में redirect करता है।
  • 2> सिर्फ stderr redirect करता है; 2>&1 stderr को जहां भी stdout currently point करता है उसमें merge करता है।
  • &>file (bash-specific) एक step में stdout और stderr दोनों को file में redirect करता है।

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.