Bash Error Message To Stderr
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 Learn more about
How To Redirect Stderr And Stdout To A File
Stack Overflow the company Business Learn more about hiring developers or posting ads with bash standard error us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is
Bash Standard Error To Variable
a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up echo that outputs to stderr up vote 468 down vote favorite 81 Is bash redirect standard error to /dev/null there a standard Bash tool that acts like echo but outputs to stderr rather than stdout? I know I can do echo foo 1>&2 but it's kinda ugly and, I suspect, error prone (e.g. more likely to get edited wrong when things change). bash share|improve this question edited Aug 19 '14 at 22:36 Steven Penny 1 asked Jun 7 '10 at 14:36 BCS 25.4k41145245 add a comment| 13 Answers 13 bash pipe standard error active oldest votes up vote 463 down vote accepted This question is old, but you could do this, which facilitates reading: >&2 echo "error" The operator '>&2' literally means redirect the address of file descriptor 1 (stdout) to the address of file descriptor 2 (stderr) for that command. depending on how deeply you want to understand it, read this: http://wiki.bash-hackers.org/howto/redirection_tutorial To avoid interaction with other redirections use subshell (>&2 echo "error") share|improve this answer edited Apr 8 at 7:25 Mateusz Konieczny 415319 answered May 8 '14 at 18:59 Marco Aurelio 4,8941913 80 alias errcho='>&2 echo' –BCS Aug 5 '14 at 21:29 3 @macmac, could you offer an explanation of this syntax or a link to more information? –allonhadaya Sep 25 '14 at 23:13 7 @allonhadaya, the operator '>&2' literally means redirect the address of file descriptor 1 (stdout) to the address of file descriptor 2 (stderr) for that command. depending on how deeply you want to understand it, read this: wiki.bash-hackers.org/howto/redirection_tutorial –John Morales Jan 6 '15 at 14:21 13 @BCS I dunno about using an alias in a shell script. It would probably be safer to use errcho(){ >&2 echo $@; } –Braden Best Jul 13 '15 at 21:52 33 In the ne
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
Bash Echo To Standard Error
site About Us Learn more about Stack Overflow the company Business Learn more bash write to standard error about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Bash Print To Standard Error
Dismiss Join the Stack Overflow 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 What method http://stackoverflow.com/questions/2990414/echo-that-outputs-to-stderr should I use to write error messages to 'stderr' using 'printf' in a bash script? up vote 14 down vote favorite I want to direct the output of a printf in a bash script to stderr instead of stdout. I am not asking about redirecting either stderr or stdout from where ever they are currently routed. I just want to be able to send http://stackoverflow.com/questions/10963653/what-method-should-i-use-to-write-error-messages-to-stderr-using-printf-in-a the output from a printf to stderr instead of to the default of stdout. I experimented a little and found that appending 1>&2 to the printf, as shown in the example below, appears to do what I want. However, I no experience using bash. So my primary question is if there is a "better" way to do this in bash? By "better" I mean is there another way to do this which is more commonly used, more conventional, or more idiomatic? How would a more experienced bash programmer do it? #!/bin/bash printf "{%s} This should go to stderr.\n" "$(date)" 1>&2 printf "[(%s)] This should go to stdout.\n" "$(date)" I also have a secondary question. I am asking it not so much because I need to know, but more because I am just curious and would like to have a better understanding about what is happening It seems the above will only work when it runs inside a shell script. It does not appear to work when I try it from a command line. Here is an example of what I mean. irrational@VBx64:~$ printf "{%s} Sent to stderr.\n" "$(date)" 1>&2 2> errors
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the http://unix.stackexchange.com/questions/164217/write-to-stderr workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only standard error 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 Write to stderr up vote 5 down vote favorite I'm using stderred to color all output streamed to stderr red. It's working fine. But when I write my own bash script and throw to standard error an error with echo 'error' 1>&2, it doesn't color the output in red. I reckon this is, because the command simply redirects the output to wherever the stderr file descriptor points to, but doesn't properly mark the message as belonging to stderr. Is that so? How can I properly write to stderr in bash? bash stderr share|improve this question asked Oct 25 '14 at 17:28 pfnuesel 1,0271926 2 for the record, you can also do >&2 echo error according to this answer. –Erik Allik Dec 14 '15 at 14:52 add a comment| 3 Answers 3 active oldest votes up vote 6 down vote accepted It appears that the program is re-writting the various write() functions to detect whether you are printing to file descriptor 2 and then adding the relevant escape codes to make the output red at the terminal. Unfortunately, in shell, when you do something like echo "foo" 1>&2 The function will still be calling write (or some other similar system call) on file descriptor 1. The output appears on fd 2 since file descriptor 1 has been dupped t