Technical skill

Bash syntax: Redirections

Send command input and output somewhere else

When you run a command, Bash normally connects its input and output to your terminal. Redirection changes those connections: a command can read from a file, write to a file, or send error messages somewhere separate. Bash is the program reading these shell commands. It opens or duplicates the requested connections before starting the command.

For example, these commands sort names from a file, replace a result file, and then append another line:

sort < names.txt
printf 'finished
' > result.txt
printf 'another line
' >> result.txt

< sends a file to the command as input. > sends standard output to a file, creating it or replacing its contents; >> adds output at the end. The shell normally truncates the destination as soon as it opens it, before it knows whether the command will succeed. Check the path first if the old contents matter. In Bash, set -o noclobber prevents accidental replacement of regular files; >| file overrides that protection for one redirect.

Choose which stream to redirect

A command has standard input (descriptor 0), standard output (1), and standard error (2). A descriptor is a numbered connection a program reads from or writes to. By default, input comes from the keyboard and both output streams go to the terminal. Put a number before a redirect to change a particular stream: command 2>errors.txt sends error messages to a file. command >/dev/null 2>&1 sends both output streams to /dev/null, a special destination that discards data.

Bash applies redirects from left to right, which changes the meaning of these commands:

command >all.log 2>&1
command 2>&1 >output.log

The first sends standard output to all.log, then copies that connection to standard error. The second first copies the original standard output to standard error, then sends standard output to output.log. The earlier copy is not changed by the later redirect.

Feed text or keep a descriptor open

A here-document feeds several lines of text to a command until it sees a chosen ending word:

cat <<'EOF'
The shell passes this text to cat.
Variables such as $HOME are not expanded.
EOF

Quoting the ending word prevents variable, command, and arithmetic expansion in the text. Without the quotes, Bash performs the expansions described in its manual. A here-string, <<< word, sends one expanded word followed by a newline.

2>&1 copies descriptor 1 onto descriptor 2 at that moment; it does not make the two permanently follow each other if descriptor 1 changes later. Bash can also reserve a descriptor in the current shell, as in exec 3>trace.log, for later commands to use.

Redirection is parsed before the program starts. Bash adds forms such as &> and here-strings; scripts for other shells should use POSIX redirection syntax. Check help or man bash for the shell interpreting a script. When debugging, keep normal output and diagnostics separate until a combined log is useful. Use printf to test redirection order before redirecting a command that changes files.

Interactive exercises

Materials

Skillsets
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful