When I run the following command in csh, I got nothing, but it works in bash.
Is there any equivalent in csh which can redirect the standard error to standard out?
somecommand 2>&1
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
cshshell has never been known for its extensive ability to manipulate file handles in the redirection process.You can redirect both standard output and error to a file with:
but that’s not quite what you were after, redirecting standard error to the current standard output.
However, if your underlying operating system exposes the standard output of a process in the file system (as Linux does with
/dev/stdout), you can use that method as follows:This will force both standard output and standard error to go to the same place as the current standard output, effectively what you have with the
bashredirection,2>&1.Just keep in mind this isn’t a
cshfeature. If you run on an operating system that doesn’t expose standard output as a file, you can’t use this method.However, there is another method. You can combine the two streams into one if you send it to a pipeline with
|&, then all you need to do is find a pipeline component that writes its standard input to its standard output. In case you’re unaware of such a thing, that’s exactly whatcatdoes if you don’t give it any arguments. Hence, you can achieve your ends in this specific case with:Of course, there’s also nothing stopping you from running
bash(assuming it’s on the system somewhere) within acshscript to give you the added capabilities. Then you can use the rich redirections of that shell for the more complex cases wherecshmay struggle.Let’s explore this in more detail. First, create an executable
echo_errthat will write a string tostderr:Then a control script
test.cshwhich will show it in action:The
echoof the PID andpsare simply so you can ensure it’scshrunning this script. When you run this script with:(the initial redirection is set up by
bashbeforecshstarts running the script), and examine theout/errfiles, you see:You can see there that the
test.cshprocess is running in the C shell, and that callingbashfrom within there gives you the fullbashpower of redirection.The
2>&1in thebashcommand quite easily lets you redirect standard error to the current standard output (as desired) without prior knowledge of where standard output is currently going.