Bash Hide Error Output
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us bash silent Learn more about Stack Overflow the company Business Learn more about hiring developers or
Bash Hide Output Of Command
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow bash error output to file Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to silence output in a bash script? bash error output to variable up vote 71 down vote favorite 11 I have a program that outputs to stdout and would like to silence that output in a bash script while piping to a file. For example, running the program will output: % myprogram % WELCOME TO MY PROGRAM % Done. I want the following script to not output anything to the command-line: #!/bin/bash myprogram > sample.s bash share|improve this question edited
Bash Error Output To Dev/null
Jan 26 '14 at 18:17 John Kugelman 173k36306384 asked Feb 18 '10 at 22:53 Sam 356133 From what I recall, redirecting output to a file causes it to not be echoed to the terminal. What's not working for you? –Anon. Feb 18 '10 at 22:55 serverfault.com/questions/41964/… –Ciro Santilli 烏坎事件2016六四事件 法轮功 Jun 29 at 21:31 add a comment| 6 Answers 6 active oldest votes up vote 92 down vote If it outputs to stderr as well you'll want to silence that. You can do that by redirecting file descriptor 2: # Send stdout to sample.s, stderr to sample.err myprogram > sample.s 2> sample.err # Send both stdout and stderr to sample.s myprogram &> sample.s # New bash syntax myprogram > sample.s 2>&1 # Older sh syntax # Log output, hide errors. myprogram > sample.s 2> /dev/null share|improve this answer answered Feb 18 '10 at 22:58 John Kugelman 173k36306384 4 The "&>" line is a shorter version of what I just posted. I haven't come across that shortcut before. Upvoting. –chradcliffe Feb 18 '10 at 23:02 It's new in Bash 4. –Dennis Williamson Feb 18 '10 at 23:23 4 &> is a lot old
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about bash redirect error output hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask bash suppress error output Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join
Bash Capture Error Output
them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top suppress stderr messages in a bash script up vote http://stackoverflow.com/questions/2292847/how-to-silence-output-in-a-bash-script 6 down vote favorite 1 Consider the following (slightly silly) script name 'test1.sh': #/bin/bash # sleep 10 & echo sleep pid = $! pkill sleep When I run it, I get not only the output of the echo, but bash's reporting of the death of sleep on stderr: $ ./test1.sh sleep pid = 3551 ./test1.sh: line 5: 3551 Terminated sleep 10 In this case, I'd like to suppress the printout to stderr. I know I can do it on the http://unix.stackexchange.com/questions/184804/suppress-stderr-messages-in-a-bash-script command line, as in: $ ./test1.sh 2> /dev/null ... but is there a way to suppress it from within the script? (I know I could wrap it in a second script and have the wrapper redirect it, but there must be something easier...) bash shell-script stderr share|improve this question asked Feb 14 '15 at 6:16 fearless_fool 143115 did you try adding the redirect 2> /dev/null after the pkill sleep ? –rahul Feb 14 '15 at 6:22 @rahul: yes I did -- pkill isn't generating the message, bash is. –fearless_fool Feb 14 '15 at 6:23 I used kill instead of pkill and do not get the stderr. strange.. –rahul Feb 14 '15 at 6:32 @rahul: could it be a built in vs non-built in thing? Did you try it with pkill as well? –fearless_fool Feb 14 '15 at 6:38 yes, i believe it is. I get the same error with pkill, but not with kill. While using kill, i used the pid instead of the proc name. –rahul Feb 14 '15 at 7:12 add a comment| 2 Answers 2 active oldest votes up vote 15 down vote accepted You're right; pkill isn't generating the message, bash is. You suggest that $ ./test1.sh 2> /dev/null is a possible solution. As UVV points out, the equivalent action from within the script is exec 2> /dev/null This redirects the stderr for the
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this http://askubuntu.com/questions/474556/hiding-output-of-a-command site About Us Learn more about Stack Overflow the company Business Learn more http://mindspill.net/computing/linux-notes/hiding-stderr-or-stdout/ about hiring developers or posting ads with us Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The error output best answers are voted up and rise to the top Hiding output of a command up vote 7 down vote favorite 1 I have a script where it checks whether a package is installed or not and whether the port 8080 is being used by a particular process or not. I am not experienced at all with bash, so I did something like this: if dpkg bash error output -s net-tools; then if netstat -tlpn | grep 8080 | grep java; then echo "Shut down server before executing this script" exit fi else echo "If the server is running please shut it down before continuing with the execution of this script" fi # the rest of the script... However when the script is executed I get both the dpkg -s net-tools and the netstat -tlpn | grep 8080 | grep java outputs in the terminal, and I don't want that, how can I hide the output and just stick with the result of the ifs? Also, is there a more elegant way to do what I'm doing? And is there a more elegant way to know what process is using the port 8080 (not just if it's being used), if any? bash scripts share|improve this question asked May 30 '14 at 17:44 dabadaba 245414 add a comment| 2 Answers 2 active oldest votes up vote 10 down vote accepted To hide the output of any command usually the stdout and stderr are redirected to /dev/null. command > /dev/null 2>&1 Explanation: 1.command > /dev/null: redirects the output of command(stdout) to /dev/null 2.2>&1: redirects stderr to stdou
produced by a program, redirect them to /dev/null, a special file that swallows all data written to it. Hide stdout: the_program > /dev/null (Shorthand for the_program 1> /dev/null) Hide stderr: the_program 2> /dev/null Hide stdout and stderr: the_program &> /dev/null References: What does “> /dev/null 2>&1″ mean? How to hide the output of a shell application in Linux? BASH Programming - Introduction HOW-TO: All about redirection Last modified: 10/03/2012 Tags: bash Site Info mindspill.net © Stephan Dale. Privacy policy Accessibility How to read this page Access keys Accessibility statement Go to top Go to top