stdout (Standard Out)
100%

Text-Fu · Lesson 1

stdout (Standard Out)

Learn how standard output flows to the terminal and how Bash redirects it to files.

Programs communicate through input/output streams. Standard output, abbreviated stdout, is the stream a program normally uses for its regular results. In a terminal, the shell initially connects that stream to the terminal display.

Writing to Standard Output

The echo command writes its arguments to stdout:

$ echo Hello World
Hello World

Stdout is file descriptor 1, a number that becomes useful when you redirect more than one stream. Programs can also have standard input, or stdin, and standard error, or stderr; the next lessons examine those streams.

Without redirection, where does echo Hello World normally send its regular output in an interactive terminal?

Replacing a File with >

Bash interprets > as an output-redirection operator. It opens the destination file and connects the command's stdout to it:

$ echo Hello World > peanuts.txt

The text no longer appears on the terminal because stdout goes to peanuts.txt. If the file is missing, the shell creates it. If it exists, the shell truncates it before the command writes, so the previous contents are lost.

Use cat to inspect the result:

$ cat peanuts.txt
Hello World

notes.txt already contains text. What does echo new > notes.txt do?

Because the shell opens the destination before the command runs, verify the pathname before pressing Enter. A misspelled or unintended existing file can be truncated even if the command later fails.

Appending to a File with >>

Use >> when new stdout should be added after a file's existing contents:

$ echo Another line >> peanuts.txt
$ cat peanuts.txt
Hello World
Another line

Like >, >> creates a missing destination. The difference is how an existing file is opened: >> appends instead of truncating.

Which command adds Finished to the end of status.log without erasing existing content?

Redirection Belongs to the Shell

The shell recognizes > and >>, removes those operators from the arguments passed to the program, opens the file, and arranges the stream connection. The command itself simply writes to stdout as usual.

This means the same redirection syntax works with many commands:

$ pwd > current-directory.txt
$ ls -la >> directory-list.txt

Who normally interprets > in pwd > current-directory.txt?

To practice standard-stream redirection, try this hands-on lab:

  1. Redirecting Input and Output in Linux - Practice controlling data flow from commands by manipulating standard output (stdout), standard error (stderr), and standard input (stdin) using operators like >, >>, 2>, and the tee command.

Lesson complete

You finished stdout (Standard Out)

You can now redirect a command's standard output without confusing replacement and append behavior.

  • Recognize stdout as the stream for regular command results.

  • Replace a file's contents with >.

  • Preserve existing contents and append with >>.

  • Verify a destination before the shell opens it.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Text-Fu