← Back to Bash Course | Chapter 3: Control Flow | Lesson 5 of 14

Redirections (> >> 2>) का उपयोग

Redirection यह चुनने जैसा है कि एक tap से पानी कहां जाता है: sink के बजाय एक bucket (एक file) में। एक arrow पुराने पानी पर डालता है, दो arrows ऊपर add करते हैं, और एक और arrow सिर्फ error messages पकड़ता है।

In this page:

  1. Redirections (> >> 2>)
Syntax
bash
command > file      # overwrite stdout
command >> file     # append stdout
command 2> file     # redirect stderr

Redirections (> >> 2>)

> standard output को एक file में redirect करता है, इसे overwrite करते हुए; >> overwrite करने के बजाय append करता है। 2> standard error को standard output से अलग redirect करता है, और 2>&1 error को output की same stream में merge करता है।

Redirection logging और normal output को error messages से अलग करने के लिए essential है।

Warning: > target file को तुरंत truncate करता है, भले ही बाद में आने वाला command चलने में fail हो।

उदाहरण: Redirections (> >> 2>)

bash
#!/bin/bash
echo "First line" > output.txt
echo "Second line" >> output.txt
cat output.txt

ls /no/such/path 2> error.txt
cat error.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. >> मतलब होने पर > उपयोग करना, जो append करने के बजाय file overwrite करता है।
  2. File में redirection से पहले 2>&1 लिखना, जैसे cmd 2>&1 > out, इसलिए errors अब भी terminal पर जाती हैं।
  3. एक command में same file पढ़ना और लिखना, जैसे sort file > file, जो इसे पहले empty कर देता है।

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.