Search code examples
c++stdoutkshstderrtcsh

Behavior of stderr and stdout differs


stdout and stderr gives different result when run through tcsh and ksh. As per my understanding the behavior in ksh is correct while in tcsh it is not. My aim was to understand the difference between cout and cerr and so i wrote a small c++ program :

    std::cout<<"this goes in standard output\n";
    std::cerr<<"This goes in error output\n";

and produced out as the executable out of it.

I made two shell scripts one for ksh and other for tcsh as below:

:~/CPP/TEST [124]$ cat test
#!/bin/tcsh

      out 2>>error.log

:~/CPP/TEST [125]$ cat test1
#!/usr/bin/ksh

out 2>>error.log

:~/CPP/TEST [129]$ ./test
This goes in error output            //WRONG
:~/CPP/TEST [130]$ cat error.log
this goes in standard output         //WRONG
:~/CPP/TEST [131]$ ./test1
this goes in standard output         //OK
:~/CPP/TEST [132]$ cat error.log
this goes in standard output
This goes in error output            //OK

Isn't the behavior of stderr, stdout supposed to be the same across shells?


Solution

  • According to this tcsh manual page you need to use >>& to redirect the error output (as well as standard output)

    That manual also states

    The shell cannot presently redirect diagnostic output without also redirecting standard output, but (command >> output-file) >& error-file is often an acceptable workaround.

    To summarize, in tcsh there's really no way to redirect the error output only to a file.