Unix Input/Output Redirection
Перевод: Перенаправление ввода/вывода в Unix
Error Output STDERR Redirection
Input/output redirection is a fundamental functionality in Unix and Linux. It’s a great way of manipulating data exchange between commands that you run.
There’s lots of examples here and this probably calls for a Unix Input/Output Redirection Reference page (I’ll create it soon).
Today I just want to show you an example of using input/output redirection – follow my steps and let me know if you get the same results. Any questions – please get in touch and I’ll update this port and the Unix Input/Output Redirect reference.
Standard Output Redirect
Let’s say we want to create a simple text file with a message “Hello”. One way to do this would be to output the Hello message using echo command, and then to redirect its standard output using the > redirection:
Basic use for redirection is: you run any commands you like, and then finish the command line with the > sign that invokes redirection, and specify the file where redirection should be written to.
USEFUL: Such standard output is called STDOUT.
If we check contents of the /tmp/try.hello file using cat command now, we can see our Hello:
Since we can redirect output of any commands like this, we can redirect the result of this cat /tmp/try.hello command into another file, perhaps /tmp/try.hello2, and it will then also contain our Hello message:
Standard Input Redirect
Similar to Standard Output, there’s also a Standard Input – called STDIN. What it does is sources content of a specified file to use it for input to whatever you’re running.
So we use the < sign to redirect (take) input from a file. For instance:
Now, this is the simplest example and not the most useful one: most commands in Unix expect input file as one of the first parameters anyway. So we don’t really have to forward input like this – we could just run “cat /tmp/try.hello”.
But it’s important to recognise the difference: in this example with STDIN redirection above, cat command is not aware of any input files. It’s run without parameters and as such expects someone to type input or source it using redirection just like we did.
Standard Error Output Redirect
Now, what happens if the command you run generates an error? That’s not a standard command behaviour or standard output. It’s an error message, or standard error output: STDERR.
What this mean is that Unix/Linux is rather clever – error messages will be treated as a separate error destination. So even though in your console you’ll get both errors and standard output, the redirection will treat them separately.
Here’s an example. I’m trying to cat a non-existent file:
This "cat: /tmp/try.hello3: No such file or directory"
is an error message, not the standard output. That’s why, when I’m redirecting it to a file using standard output redirection, nothing is captured and put into the redirection output file:
Pretty cool, huh?
In order to redirect specifically error messages, we need to use special form of redirection, for STDERR error messages. We use number 2 before the redirection symbol, which refers to STDERR:
Two things happened:
- Our command returned no output. Because all of the result (standard error it generated) got forwarded to the
/tmp/redirected.out
file
- The
/tmp/redirected.out
file now contains our error message
Think this is enough for one post. Will copy most of this into the Unix redirects reference page and come back some other day with more on this.
Have fun!
See Also
Error Output STDERR Redirection
Input/output redirection is a fundamental functionality in Unix and Linux. It’s a great way of manipulating data exchange between commands that you run.
There’s lots of examples here and this probably calls for a Unix Input/Output Redirection Reference page (I’ll create it soon).
Today I just want to show you an example of using input/output redirection – follow my steps and let me know if you get the same results. Any questions – please get in touch and I’ll update this port and the Unix Input/Output Redirect reference.
Standard Output Redirect
Let’s say we want to create a simple text file with a message “Hello”. One way to do this would be to output the Hello message using echo command, and then to redirect its standard output using the > redirection:
Basic use for redirection is: you run any commands you like, and then finish the command line with the > sign that invokes redirection, and specify the file where redirection should be written to.
USEFUL: Such standard output is called STDOUT.
If we check contents of the /tmp/try.hello file using cat command now, we can see our Hello:
Since we can redirect output of any commands like this, we can redirect the result of this cat /tmp/try.hello command into another file, perhaps /tmp/try.hello2, and it will then also contain our Hello message:
Standard Input Redirect
Similar to Standard Output, there’s also a Standard Input – called STDIN. What it does is sources content of a specified file to use it for input to whatever you’re running.
So we use the < sign to redirect (take) input from a file. For instance:
Now, this is the simplest example and not the most useful one: most commands in Unix expect input file as one of the first parameters anyway. So we don’t really have to forward input like this – we could just run “cat /tmp/try.hello”.
But it’s important to recognise the difference: in this example with STDIN redirection above, cat command is not aware of any input files. It’s run without parameters and as such expects someone to type input or source it using redirection just like we did.
Standard Error Output Redirect
Now, what happens if the command you run generates an error? That’s not a standard command behaviour or standard output. It’s an error message, or standard error output: STDERR.
What this mean is that Unix/Linux is rather clever – error messages will be treated as a separate error destination. So even though in your console you’ll get both errors and standard output, the redirection will treat them separately.
Here’s an example. I’m trying to cat a non-existent file:
This "cat: /tmp/try.hello3: No such file or directory"
is an error message, not the standard output. That’s why, when I’m redirecting it to a file using standard output redirection, nothing is captured and put into the redirection output file:
Pretty cool, huh?
In order to redirect specifically error messages, we need to use special form of redirection, for STDERR error messages. We use number 2 before the redirection symbol, which refers to STDERR:
Two things happened:
- Our command returned no output. Because all of the result (standard error it generated) got forwarded to the
/tmp/redirected.out
file - The
/tmp/redirected.out
file now contains our error message
Think this is enough for one post. Will copy most of this into the Unix redirects reference page and come back some other day with more on this.
Have fun!