15

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.

gadgetmo
  • 831

4 Answers4

22

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.

Sooth
  • 391
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.

Craig
  • 694
  • 1
    +1 and ✔. I changed shells using sh. – gadgetmo Apr 04 '12 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. – James O'Gorman Apr 06 '12 at 23:21
  • @JamesO'Gorman Good catch I updated my answer. – Craig Apr 07 '12 at 01: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.

Anthon
  • 79,293
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