Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
Are you sure you are running bash or sh? On FreeBSD csh or tcsh is often the default. –  Craig Apr 4 '12 at 16:50
    
What does echo $SHELL show? –  Karlson Apr 4 '12 at 16:51
    
@Craig I'm on csh. –  gadgetmo Apr 4 '12 at 16:59
    
@Karlson I'm on csh. –  gadgetmo Apr 4 '12 at 17:00
    
i have edited.. –  gadgetmo Apr 4 '12 at 17:00
add comment

3 Answers 3

up vote 6 down vote accepted

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.

share|improve this answer
    
+1 and ✔. I changed shells using sh. –  gadgetmo Apr 4 '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 6 '12 at 23:21
    
@JamesO'Gorman Good catch I updated my answer. –  Craig Apr 7 '12 at 1:35
add comment

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.

share|improve this answer
    
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 at 23:46
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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