Unix Input/Output Redirection

Перевод: Перенаправление ввода/вывода в Unix

Unix Input/Output Redirection 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:

greys@maverick:~ $ echo Hello > /tmp/try.hello

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:

greys@maverick:~ $ cat /tmp/try.hello
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:

greys@maverick:~ $ cat /tmp/try.hello > /tmp/try.hello2 greys@maverick:~ $ cat /tmp/try.hello2 Hello

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:

greys@maverick:~ $ cat < /tmp/try.hello
Hello

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:

greys@maverick:~ $ cat /tmp/try.hello3
cat: /tmp/try.hello3: No such file or directory

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:

greys@maverick:~ $ cat /tmp/try.hello3 > /tmp/redirected.out
cat: /tmp/try.hello3: No such file or directory
greys@maverick:~ $ cat /tmp/redirected.out
greys@maverick:~ $

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:

greys@maverick:~ $ cat /tmp/try.hello3 2> /tmp/redirected.out greys@maverick:~ $ cat /tmp/redirected.out 
cat: /tmp/try.hello3: No such file or directory 

Two things happened:

  1. Our command returned no output. Because all of the result (standard error it generated) got forwarded to the /tmp/redirected.out file
  2. 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




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets