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!
'
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!
' > myfile
$

Notice that when we run the printf command in the preceding example, we don't get any output on the screen before our prompt shows again. However, if we run cat with myfile as its argument afterward, we can see where the command's output went; the file contains the output that the command would otherwise have written to our terminal:

$ cat myfile
Hello, file!

When you use output redirection to a file, the file is created even if the command had no output at all; if there's no output, it's created empty, or with a single terminating newline, depending on what the command emitted:

$ printf '' > myemptyfile
$ wc -c myemptyfile
0
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.63.5