Redirecting output

When you enter a simple command that produces output in an interactive Bash session, such as a call to printf, it writes it straight back to your terminal so that you can read it, before returning you to your prompt:

$ printf 'Hello, terminal!\n'
Hello, terminal!
$

In both an interactive session and a script, you will often want to do something with the output besides printing it to the screen, particularly if you need to save the data permanently for later use. The simplest means of doing this is to save the output to a file.

To accomplish this, we can use one of Bash's redirection operators, the right angled bracket, >, followed by a filename path:

$ printf 'Hello, file!\n' > myfile
$

Notice that when we run the printf ...

Get Bash Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.