/dev/null से Output हटाना
In this page:
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
#!/bin/bash
echo "you will not see this" > /dev/null
echo "but this still prints"
Login to try C/C++/Java/PHP code in the editor
Silencing stderr Only
सिर्फ file descriptor 2 को /dev/null में redirect करना error messages को hide करता है जबकि normal stdout output visible रखते हुए, जो useful है जब एक command की errors expected और not actionable हों।
उदाहरण: Silencing stderr Only
#!/bin/bash
ls not_a_real_file 2> /dev/null
echo "script continued despite the hidden error, exit was: $?"
Login to try C/C++/Java/PHP code in the editor
Silencing Everything
&> /dev/null (या > /dev/null 2>&1) stdout और stderr दोनों discard करता है, जो common है जब एक script सिर्फ यह care करती है कि एक command succeed हुआ या नहीं, $? से checked।
उदाहरण: Silencing Everything
#!/bin/bash
if command -v ls > /dev/null 2>&1; then
echo "ls is available"
fi
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
while read -r line < /dev/null; do
echo "never printed: $line"
done
echo "loop finished immediately because /dev/null is empty"
Login to try C/C++/Java/PHP code in the editor
- सिर्फ stdout को
/dev/nullमें redirect करना और surprised होना कि error messages अब भी दिखते हैं; stderr को अपनी खुद की redirection चाहिए। /dev/nullसे meaningful data वापस पढ़ने की कोशिश करना; यह हमेशा empty input produce करता है, यह एक one-way discard है।/dev/nullमें redirect करना जब आप असल में बाद में inspect करने के लिए output capture करना चाहते थे, ज़रूरी information खो देना।
/dev/nullइसमें लिखी गई किसी भी चीज़ को discard करता है और हमेशा empty पढ़ता है।cmd > /dev/nullstdout silence करता है;cmd 2> /dev/nullstderr silence करता है;cmd &> /dev/nullदोनों silence करता है।- Output silence करना common है जब आप सिर्फ एक command के exit status की care करते हों, इसके text की नहीं।
/dev/null<के साथ redirect होने पर एक empty input source के रूप में भी काम करता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- Function Basics
- The read Command and Here-Strings
- Function Arguments
- stdin, stdout, stderr and File Descriptors
- Return Values
- Redirection Operators
- Local Variables
- Pipes and Chaining Commands
- Recursive Functions
- Here-Documents (<<EOF)
- Function Libraries
- The tee Command
- Discarding Output with /dev/null