Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

From my understanding, the following should send 'test' on standard error in tcsh:

echo test >&2

However, it instead writes 'test' to a file named 2, and when I look through my history, I find that what actually executed was

echo test > & 2

I'm not sure what layer is inserting those spaces, but can I stop it somehow? If not, I guess I could always use > /dev/stderr instead.

share|improve this question
    
echo test >&2 prints to stderr. At least in my bash. –  Oliver Salzburg May 23 '12 at 14:43
    
BTW, using > as a shell indicator is pretty confusing in your code snippets in the current context ;) –  Oliver Salzburg May 23 '12 at 14:44
    
I assumed bash at first as well but it's tagged tsch. –  Dave Forgac May 23 '12 at 14:44
    
Sorry about the confusion, hopefully my question is more clear now. If I could use bash here, I would, since it works for me. –  Nate Parsons May 23 '12 at 15:22
    
Could you quote the redirect? echo test '>&2'? No idea if that works, and don't have tcsh to try it. –  kevlar1818 May 23 '12 at 15:36
show 2 more comments

3 Answers

up vote 3 down vote accepted

Concerning csh, from http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/:

Along these same lines, you can't direct error messages in csh scripts out stderr as is considered proper. In the Bourne shell, you might say:

echo "$0: cannot find $file" 1>&2

but in the csh, you can't redirect stdout out stderr, so you end up doing something silly like this:

sh -c 'echo "$0: cannot find $file" 1>&2'

I did some quick tries with tcsh, and as suspected it appears to be true for that as well (tcsh is supposed to be fully compatible with csh).


To address your formulated question: it wouldn't matter if you could make tcsh not "insert spaces" since it will not interpret that sequence in the intended way anyway.

share|improve this answer
add comment

EDIT:

I didn't see this was tsch. See the "Input/output" section of man tsch for details including this:

Diagnostic output may be directed through a pipe with the standard output. Simply use the form |& rather than just |.

The shell cannot presently redirect diagnostic output without also redirecting standard output, but (command > output-file) >& error-file is often an acceptable workaround. Either output-file or error-file may be /dev/tty to send out-put to the terminal.

share|improve this answer
1  
Thanks for pointing that out, but I'm not sure it applies. I don't want to redirect the diagnostic output (stderr?) of a process, instead I want the normal output to appear on stdout. –  Nate Parsons May 23 '12 at 15:30
    
@drhorrible: It's essentially the same thing. What you're doing is starting the echo process and redirecting its output to stderr. Thus, printing to stderr effectively :) –  Oliver Salzburg May 24 '12 at 9:35
    
That's not how it seems to work. (echo test > out) >& err writes 'test' to a file called 'out' and creates an empty file 'err' on my machine, at least. –  Nate Parsons May 24 '12 at 17:08
add comment

Daniel Andersson's solution is great and will work well if the command you want to run can be run from sh (and, therefore, a subshell, which will be unable to change the directory or modify the environment of your current shell). If you want to convert stdout to stderr but keep the command execution in the current shell, you can use bash only for the stdout-to-stderr redirection, like this:

echo "An error message" | bash -c "cat - 1>&2"

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.