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

/dev/null से Output हटाना

/dev/null एक special file है जो इसमें लिखी गई किसी भी चीज़ को चुपचाप फेंक देता है, जो उस output को silence करने के लिए उपयोगी है जिसकी आपको परवाह नहीं।
Syntax
bash
command > /dev/null
command 2> /dev/null
command > /dev/null 2>&1

Silencing stdout

एक command के stdout को /dev/null में redirect करना इसके normal output को पूरी तरह discard कर देता है जबकि command को सामान्य रूप से चलने और इसका exit status set करने देते हुए।

उदाहरण: Silencing stdout

bash
#!/bin/bash
echo "you will not see this" > /dev/null
echo "but this still prints"

Silencing stderr Only

सिर्फ file descriptor 2 को /dev/null में redirect करना error messages को hide करता है जबकि normal stdout output visible रखते हुए, जो useful है जब एक command की errors expected और not actionable हों।

Warning: Errors silence करना असली bugs hide कर सकता है; इसे सिर्फ तब करें जब आपके पास उस error को expect और ignore करने का एक specific reason हो।

उदाहरण: Silencing stderr Only

bash
#!/bin/bash
ls not_a_real_file 2> /dev/null
echo "script continued despite the hidden error, exit was: $?"

Silencing Everything

&> /dev/null (या > /dev/null 2>&1) stdout और stderr दोनों discard करता है, जो common है जब एक script सिर्फ यह care करती है कि एक command succeed हुआ या नहीं, $? से checked।

उदाहरण: Silencing Everything

bash
#!/bin/bash
if command -v ls > /dev/null 2>&1; then
    echo "ls is available"
fi

Using /dev/null as Empty Input

/dev/null को < के साथ एक source के रूप में भी उपयोग किया जा सकता है, जिस case में यह हमेशा immediately end-of-file signal करता है, एक empty input stream provide करते हुए।

उदाहरण: Using /dev/null as Empty Input

bash
#!/bin/bash
while read -r line < /dev/null; do
    echo "never printed: $line"
done
echo "loop finished immediately because /dev/null is empty"
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. सिर्फ stdout को /dev/null में redirect करना और surprised होना कि error messages अब भी दिखते हैं; stderr को अपनी खुद की redirection चाहिए।
  2. /dev/null से meaningful data वापस पढ़ने की कोशिश करना; यह हमेशा empty input produce करता है, यह एक one-way discard है।
  3. /dev/null में redirect करना जब आप असल में बाद में inspect करने के लिए output capture करना चाहते थे, ज़रूरी information खो देना।
चैप्टर सारांश
  • /dev/null इसमें लिखी गई किसी भी चीज़ को discard करता है और हमेशा empty पढ़ता है।
  • cmd > /dev/null stdout silence करता है; cmd 2> /dev/null stderr silence करता है; cmd &> /dev/null दोनों silence करता है।
  • Output silence करना common है जब आप सिर्फ एक command के exit status की care करते हों, इसके text की नहीं।
  • /dev/null < के साथ redirect होने पर एक empty input source के रूप में भी काम करता है।

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.