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

/dev/null का उपयोग

/dev/null एक magic trash chute जैसा है जो आप जो भी इसमें डालें उसे बिना कोई trace छोड़े निगल जाता है। आप noisy messages वहां भेजते हैं जब आप उन्हें देखना नहीं चाहते।

In this page:

  1. /dev/null
Syntax
bash
command > /dev/null
command 2> /dev/null

/dev/null

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

/dev/null से पढ़ना हमेशा तुरंत बिना किसी data के end-of-file return करता है।

Note: command > /dev/null 2>&1 "इसे चलाएं और इसका सारा output छुपाएं" के लिए standard idiom है।

उदाहरण: /dev/null

bash
#!/bin/bash
echo "This will be discarded" > /dev/null
echo "This still prints normally"

ls /no/such/path > /dev/null 2>&1
echo "The error above was silenced. Exit code: $?"
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. > /dev/null से redirect करना और फिर भी errors देखना, क्योंकि आपको 2>&1 या 2> /dev/null भी चाहिए।
  2. 2>&1 > /dev/null को गलत order में लिखना, इसलिए errors अब भी terminal पर दिखती हैं।
  3. एक command का सारा output छुपाना और फिर यह पता न कर पाना कि यह fail क्यों होता है।

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.