Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-20-2015, 09:37 AM   #1
LQ Newbie
Registered: Feb 2015
Posts: 1

Rep: Reputation: Disabled

I am learning shell scripting. I came across "> /dev/null" many times, As a beginner I know that means "redirecting output to trash. WHAT IS THE SIGNIFICANCE OF IT IN BELOW SCRIPT?.
example code:

while read ip name
echo $ip | grep "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" > /dev/null
if [ "$?" -eq "0" ]; then
echo "$name is at $ip"
done < /etc/hosts
Old 06-20-2015, 09:49 AM   #2
Senior Member
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 2,367

Rep: Reputation: 957Reputation: 957Reputation: 957Reputation: 957Reputation: 957Reputation: 957Reputation: 957Reputation: 957

Used as it is in that script, it hides the STDOUT of the grep command. The if does not require STDOUT, only the return value. Sending the output to null keeps the output clean instead of sending the output of the grep to stdout on a terminal, into cron email, a log file, or some output that then becomes cluttered.

IT is a tool of the good scripter acting as butler: keeping things neat and tidy.

Incidently, the script is flawed. There are several possible ways to fix it, but it does not take into consideration that many hosts entries contain aliases, comments, and other verbiage after the name. There are also comment lines that should be ignored entirely for correctness, though they are unlikley to cause this snip to misbehave. It would also need significant editing to take IPv6 into account.

Was this from a textbook or classroom example?

Last edited by wpeckham; 06-20-2015 at 09:53 AM.
2 members found this post helpful.
Old 06-20-2015, 11:07 AM   #3
LQ Newbie
Registered: Mar 2015
Posts: 20

Rep: Reputation: Disabled
Originally Posted by nbonda View Post
I am learning shell scripting. I came across "> /dev/null" many times, As a beginner I know that means "redirecting output to trash.
As a beginner, do you also know the difference between "output" and "return code" or "exit status"? I didn't when I was new, so just in case:

Typically, output is written to the controlling terminal. (The user's own shell in this case.) You're probably used to using grep for its output: take whatever came in, apply the filter, and show us what the filter doesn't remove.

The exit status, or return code, is a number that's sent to the controlling process when a program ends. Typically, a value of "0" means that the program ended successfully, and some other number means there was some kind of error, or otherwise "not typical" exit status. Usually when we use the shell, we don't see the exit status, especially if everything is going according to plan.

Typically, the exit status isn't what we use grep for. It's a filter, and we're interested in its output. But when grep doesn't let anything through at all, that's considered an error, so the exit status is set to "1", and that can be useful sometimes, as in your case.
1 members found this post helpful.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Stopping cron job emails to root, do I use >/dev/null 2>&1 or &> /dev/null rjo98 Linux - Newbie 3 11-12-2013 11:21 AM
[SOLVED] Why is there a need for /dev/null, /dev/zero and other special device (files) in /dev suttiwit Linux - General 13 08-07-2012 01:26 AM
LXer: /dev/null And /dev/zero On Linux And Unix: What's The Difference? LXer Syndicated Linux News 0 04-29-2009 11:50 AM
Startx Permission problems on /dev/null and /dev/mem on freshly compiled Eric_Cartman Linux - Kernel 2 09-09-2007 02:42 AM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 11:51 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration