Bash redirect to file and screen

Shell Scripting

 Redirection Forms

File descriptor Name Common abbreviation Typical default

0 Standard input stdin Keyboard
1 Standard output stdout Screen
2 Standard error stderr Screen

The usual input source or output destination can be changed, as seen in the following sections.

Simple redirection

cmd > file
Send output of cmd to file (overwrite).

cmd >> file
Send output of cmd to file (append).

cmd < file
Take input for cmd from file.

cmd << text

The contents of the shell script up to a line identical to text become the standard input for cmd (text can be stored in a shell variable). This command form is sometimes called a here document. Input is typed at the keyboard or in the shell program. Commands that typically use this syntax include cat, ex, and sed. (If <<- is used, leading tabs are stripped from the contents of the here document, and the tabs are ignored when comparing input with the endof-input text marker.) If any part of text is quoted, the input is passed through verbatim. Otherwise, the contents are processed for variable, command, and arithmetic substitutions.

cmd <<< word
Supply text of word, with trailing newline, as input to cmd. (This is known as a here string, from the free version of the rc shell.)

cmd <> file
Open file for reading and writing on the standard input. The contents are not destroyed.*

cmd >| file
Send output of cmd to file (overwrite), even if the shell’s noclobber option is set.

Note: With <, the file is opened read-only, and writes on the file descriptor will fail. With <>, the file is opened read-write; it is up to the application to actually take advantage of this.

Redirection using file descriptors

cmd >&n
Send cmd output to file descriptor n.

cmd m>&n
Same as previous, except that output that would normally go to file descriptor m is sent to file descriptor n instead.

cmd >&-
Close standard output.

cmd <&n
Take input for cmd from file descriptor n.

cmd m<&n
Same as previous, except that input that would normally come from file descriptor m comes from file descriptor n instead.

cmd <&-
Close standard input.

cmd <&n-
Move file descriptor n to standard input by duplicating it and then closing the original.

cmd >&n-
Move file descriptor n to standard output by duplicating it and then closing the original.

Multiple redirection

cmd 2>file
Send standard error to file; standard output remains the same (e.g., the screen).

cmd > file 2>&1
Send both standard output and standard error to file.

cmd >& file
Same as previous.

cmd &> file
Same as previous. Preferred form.

cmd &>> file
Append both standard output and standard error to file.

cmd > f1 2> f2
Send standard output to file f1 and standard error to file f2.

cmd | tee files
Send output of cmd to standard output (usually the terminal) and to files.

cmd 2>&1 | tee files
Send standard output and error output of cmd through a pipe to tee to standard output (usually the terminal) and to files.

cmd |& tee files
Same as previous.

Bash allows multidigit file descriptor numbers without any special syntax. Most other shells either require a special syntax or do not offer the feature at all. Bash also allows {variablename} instead of a file descriptor number in redirections. In such a case, the shell uses a file descriptor number greater than nine, and assigns the value to the named shell variable.

NOTE
No space is allowed between file descriptors and a redirection symbol; spacing is optional in the other cases.

Process substitution

cmd <( command )
Run command with its output connected to a named pipe or an open file in /dev/fd, and place the file’s name in the argument list of cmd.

cmd >( command )
Run command with its input connected to a named pipe or an open file in /dev/fd, and place the file’s name in the argument list of cmd.

Process substitution is available on systems that support either named pipes (FIFOs) or accessing open files via filenames in /dev/fd. (This is true of all modern Unix systems.) It provides a way to create non-linear pipelines.

Special filenames

Bash recognizes several special filenames in redirections and interprets them internally, even if you have such a file on your system:

/dev/stdin
A duplicate of file descriptor zero.
/dev/stdout
A duplicate of file descriptor one.
/dev/stderr
A duplicate of file descriptor two.
/dev/fd/n
A duplicate of file descriptor n.
/dev/tcp/host/port

Bash opens a TCP connection to host, which is either a hostname or IP address, on port port and uses the file descriptor in the redirection.

/dev/udp/host/port

Bash opens a UDP connection to host, which is either a hostname or IP address, on port port and uses the file descriptor in the redirection.

Examples:

To redirect standard output to standard error:
$ echo "Usage error: see administrator" 1>&2

The following command sends output (files found) to filelist, and error messages (inaccessible files) to no_access:

$ find / -print > filelist 2>no_access

The following demonstrates how Bash assigns file descriptor

$ echo foo {foofd}> /tmp/xyzzy

foo
$ echo $foofd
11
The following sorts two files and presents the differences between
the results using the diff command:
$ diff <(sort file1) <(sort file2)

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.