LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-18-2014, 08:52 AM   #1
postcd
Member
 
Registered: Oct 2013
Posts: 527

Rep: Reputation: Disabled
Can i redirect any command to dev null?


Hello,

i have bash script and my question is if i can with peace of mind point any bash command into dev null? ( >/dev/null )

because i cant veriffy there are any errors or it really executed command. respectivelly how i can veriffy it, any advice? thx
 
Old 04-18-2014, 09:25 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
That won't redirect errors, since they normally print to stderr, not stdout.
Many commands have a return code $? which you can test to see if the command executed correctly.
 
Old 04-18-2014, 09:37 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
You can redirect any command to /dev/null. All it does is discard any output sent.

Redirection is setup by the shell usually - and that means that any executable can have stdin/stdout/stderr redirected to /dev/null. When stdin is redirected, it causes an end of file condition on a read.

This has no effect on any files opened by that executable (which includes shell scripts that may redirect things elsewhere).

As for verify - in what way do you mean by verify? If you mean "show no output" well, verification is no output... If you mean "the command had no error" then it depends on the command. Nearly all commands will exit with a 0 status on success, and non-zero status on failure. User applications might not follow that standard though. If you mean to identify WHAT error occurred, then you are out of luck. Most exit status reflect only that an error occurred (usually when the status is 1), but not what the error was - that is what stderr is used for.
 
Old 04-18-2014, 10:03 AM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
To amplify the answer about stderr and stdout:

stdout = Standard Output = File Descriptor 1
stderr = Standard Error = File Descriptor 2

It is normal to have background processes either log or go to /dev/null if you don't care about logging.

The ">" by itself redirects stdout by default and is equivalent to "1>".
The ">>" redirects stdout but appends (useful for logging) rather than overwriting.

To send BOTH stdout and stderr to the same place you can use a shortcut such as the following that would send stdout to /dev/null then redirect stderro to stdout (which has already been defined as /dev/null):
>/dev/null 2>&1

The 2>&1 says to redirect file descriptor 2 into file descriptor 1 so they both end up in /dev/null.

Note that order is important. You would not do:
2>&1 >/dev/null
as that would send stderr to wherever stdout was BEFORE you told stdout to go to /dev/null

Similarly you could go to a log file:
>/var/log/mylog 2>&1
 
  


Reply



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 On
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 anon091 Linux - Newbie 3 11-12-2013 10:21 AM
[SOLVED] Perl: redirect STDERR from system() call to /dev/null totaluser Linux - Newbie 2 11-25-2011 02:30 AM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
Redirect X windows / GUI to /dev/null? mattfr Linux - Software 9 12-20-2004 09:40 AM
Outputting Command Text to /dev/null rootking Linux - Software 4 12-19-2004 07:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

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