13

I run the following command:

pkg_add emacs-23.4,2.tbz 2> output.log

The output still displays in the terminal. When I press , I get

pkg_add emacs-23.4,2.tbz 2 > output.log

with a space before the 2.

I did not originally put this. I try

pkg_add emacs-23.4,2.tbz > output.log 2>&1

Again, when I press , spaces have been added.

Why is this happening to me?

I am running csh on FreeBSD.

0

4 Answers 4

21

I'm not sure if you are trying to hide STDERR or redirect it to STDOUT.

To redirect STDOUT to a file:

pkg_add emacs-23.4,2.tbz > stdout.log

To redirect STDOUT and STDERR to a file:

pkg_add emacs-23.4,2.tbz > & stdxxx.log

To redirect STDOUT to a file and hide STDERR:

( pkg_add emacs-23.4,2.tbz > stdout.log ) > & /dev/null

To redirect STDOUT to console and hide STDERR:

( pkg_add emacs-23.4,2.tbz > /dev/tty ) > & /dev/null

To redirect STDOUT to console and STDERR to a file:

( pkg_add emacs-23.4,2.tbz > /dev/tty ) > & stderr.log

To redirect STDOUT to a file and STDERR to a file:

( pkg_add emacs-23.4,2.tbz > stdout.log ) > & stderr.log

EDIT: The reason why this works is that the action in the ()'s happens first; Ergo, if we've redirected STDOUT, then it will no longer be available outside of the ()'s. This leaves us with just STDERR, and then we can redirect that as desired.

1
  • Hi @nikc, welcome to unix.SE. Your comment is useful and informative. I would suggest editing your answer and including it right in there so it's not so easily missed.
    – drs
    Jun 3, 2014 at 23:46
13

The 2> redirect does not work with csh or tcsh.

Use the chsh command to change your shell to /bin/sh or /usr/local/bin/bash in order to use the 2> style redirect. Note: Do not change root's shell to /usr/local/bin/bash

csh and tcsh cannot redirect standard out and error separately, but >& will redirect the combined output to a file.

3
  • 1
    +1 and ✔. I changed shells using sh.
    – gadgetmo
    Apr 4, 2012 at 17:45
  • 1
    @Craig He's running pkg_add on FreeBSD, so I'm assuming this is for the root user (/bin/csh is the default for root on FreeBSD). In this case you should not change the shell to /usr/local/bin/bash. /bin/sh is acceptable. You could also just switch to another shell after logging in as root. Apr 6, 2012 at 23:21
  • @JamesO'Gorman Good catch I updated my answer.
    – Craig
    Apr 7, 2012 at 1:35
2

I know how to do it in Csh, but using 2 shells:

csh -c 'SOME_COMMAND 1>/dev/null' |& tee file.txt

Such a way allows to redirect only stderr to file.txt, without stdout - namely what you wanted.

0

In regards to nikc's answer, to achieve the same behavior of Bourne shells, instead of /dev/tty, use a FIFO.

To redirect STDOUT to console and hide STDERR:

mkfifo stdout
( pkg install -y emacs > stdout ) >& /dev/null & cat stdout

To redirect STDERR to console and hide STDOUT:

pkg install -y emacs > /dev/null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.