Hack Like a Pro: Linux Basics for the Aspiring Hacker, Part 16 (Stdin, Stdout, & Stderror)

Linux Basics for the Aspiring Hacker, Part 16 (Stdin, Stdout, & Stderror)

Welcome back, my budding hackers!

In previous tutorials, we've looked at some of the basic commands and concepts for using Linux. Along the way, I realized that I've failed to provide you with some basic background material on the stdin, stdout, and stderror.

In human language, these are often referred to as standard input (stdin), standard output (stdout), and standard error (stderror). These represent how and where Linux sends the output from your commands (stdout), where it receives its input (stdin), and where it sendsits error messages (stderror).

Since you've now been using Linux for awhile, you realize that both standard output and standard error are sent to your computer screen. Both as the name implies, this is the standard place and not necessarily the only place. Linux let's us define where our output and error messages should go.

Before we go any further, let's take a moment to define some terms.

Standard Output (1)

Whenever you complete a command, it must know where to send the output. You might want to send it to a file, a printer, the screen, etc. The default for standard output is the computer screen. Standard output is often referred to as stdout or simply use the numeric representation of 1.

Standard Input (0)

Standard input is where the program or command gets the information it needs. By default, in Linux this is the keyboard, but can be a file, etc. Standard input is often referred to as stdin or simply represented by the numeric representation of 0.

Standard Error (2)

When we make a mistake or our program throws an error, it send the error message to stanadard error. By default, this is our computer screen. Standard error is often referred to as stderror or simply represented by the numeral 2.

When we want to direct any of these three from the command line or a script, we use the numeric representation of each, 0 for stdin, 1 for stdout, and 2 for stderr.

Step 1: List Two Directories

To demonstrate how we can use and manipulation these I/O streams, let's do a listing of two different directories, /etc/hosts and /etc/snort.

In Linux, you can do listings of more than one directory at a time. The /etc/snort directory is where our configuration file for snort resides and /etc/hosts is a directory where we can set static name resolution in Linux (I'll do a new Linux tutorial on DNS and name resolution in Linux soon).

If we wanted to see the two directories, we could type:

  • ls /etc/hosts /etc/snort

As you can see, the listing comes back to us by the standard output to our computer screen showing us the listing of both directories.

Now, let's try the same thing, but this time let's list a directory that doesn't exist, such as /etc/aircrack-ng.

  • ls /etc/hosts /etc/aircrack-ng

As you can see, our BASH shell comes back with two outputs, the standard output from etc/hosts and the standard error from the non-existent directory.

Step 2: Send Standard Output to a File

Next, let's suppose that we want to separate our standard output from our standard error. Imagine we're running a script where we don't want to see our output messages until after the script has run, but we need to see error messages on our screen immediately. We could rewrite our command as:

  • ls /etc/hosts /etc/aircrack-ng 1>goodoutput

Let's now imagine just the reverse of our previous scenario where instead we want to see our output on screen, but store our error messages to a separate file for viewing later. We could write:

  • ls /etc/hosts /etc/aircrack-ng 2>erroroutput

Now, after the command has been run, we can go back and cat the erroroutput file to view any possible error messages.

Step 3: Send Standard Output & Standard Error to Separate File

Now, let's imagine a script where we want both our standard output and standard error to be directed to separate files for viewing later. We can type:

  • ls /etc/hosts /etc/aircrack-ng 1>goodoutput 2>erroroutput

Notice that nothing comes back to our screen, neither standard output or standard error.

Step 4: Send Both Standard Output & Standard Input to Same File

Finally, what if we wanted both standard error and standard output to be written to same file? We could type:

  • ls /etc/hosts /etc/aircrack-ng >goodoutput 2>&1

Notice that I did not use the 1 before the >goodoutput as BASH defaults to stdout if no number is used.

Hope you enjoyed this quick lesson in Linux stdin, stdout, and stderror, but we have so much more coming, so keep coming back my budding hackers!

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

10 Comments

Sir, in the command
ls /etc/hosts /etc/aircrack-ng >goodoutput 2>&1
What does 2>&1 mean?
Oh, it means 2 also outputs to the same file as 1.
Can you give an example of stdin using 0?

ls directory1 directory2 0>stdinput

or

ls directory1 directory2 >stdinputAndstdoutput 0>&1

The above would be listing the contents (and any resulting errors) of DIR1 & DIR2 within a file stdinputAndstdoutput, the part 0>&1 is saying place the results of input(0) and the results of output(1) into the file we specified

In recent versions of Linux, you can redirect both stdout and stderr by using the &> operator insteaad of 2>&1

To see this in action (if your distro supports it)
ls /usr/bin/ /bin/usr/ &> outerr.txt

We can see if the command succeedeed by typing:
less outerr.txt

In my case, the file starts listing error first, then output:

ls: cannot access /bin/usr: No such file or directory
/usr/bin:
2to3
...

For me, '&>' is easier to remember than '2>&1' :)

You committed an error here: "Send Both Standard Output & Standard Input to Same File", since you meant Send Both Standard Output & Standard Error to Same File. Am I right?

I think stdin,stdout,stderror are boring...why should we use it?

These are the standard input (keyboard), standard output (monitor) and standard error (monitor). You don't have a choice, you use them every time you use Linux. On some occasions, you may want to change them, for instance, when you write a script. You may not want the error messages displayed on the screen, but rather sent to a file, for instance.

Hey, this is a good introduction to stdout & stderr, but I feel it's lacking in the stdin department. Why not alter this guide just a little bit and show some of the power of piping to show just a smidgen of the usefulness of redirecting output as input into other programs? I feel from all my time using linux that this is the #1 way of changing where a program's stdin comes from.

Thank for article but I dont get what for :)

just wanted to share this. As in step 4 you can also write the command in a more understandable form which is
ls /etc/hosts /etc/aircrack-ng 1>goodoutput 2>goodoutput

BTW awesome tutorials man really helpfull !!

Share Your Thoughts

  • Hot
  • Latest