1>filename
# Redirect stdout to file "filename."
1>>filename
# Redirect and append stdout to file "filename."
2>filename
# Redirect stderr to file "filename."
2>>filename
# Redirect and append stderr to file "filename."
&>filename
# Redirect both stdout and stderr to file "filename."
# This operator is now functional, as of Bash 4, final release.
2>&1
# Redirects stderr to stdout.
# Error messages get sent to same place as standard output.
>>filename 2>&1
bad_command >>filename 2>&1
# Appends both stdout and stderr to the file "filename" ...
2>&1 | [command(s)]
bad_command 2>&1 | awk '{print $5}' # found
# Sends stderr through a pipe.
# |& was added to Bash 4 as an abbreviation for 2>&1 |.
i>&j
# Redirects file descriptor i to j.
# All output of file pointed to by i gets sent to file pointed to by j.
>&j
# Redirects, by default, file descriptor 1 (stdout) to j.
# All stdout gets sent to file pointed to by j.
COMMAND_OUTPUT >
ls -lR > dir-tree.list
: > filename
filename
COMMAND_OUTPUT >>
1>filename
1>>filename
2>filename
2>>filename
&>filename
M>N
M>&N
#==============================================================================
LOGFILE=script.log
echo "This statement is sent to the log file, "$LOGFILE"." 1>$LOGFILE echo "This statement is appended to "$LOGFILE"." 1>>$LOGFILE echo "This statement is also appended to "$LOGFILE"." 1>>$LOGFILE echo "This statement is echoed to stdout, and will not appear in "$LOGFILE"."
ERRORFILE=script.errors
bad_command1 2>$ERRORFILE # Error message sent to $ERRORFILE. bad_command2 2>>$ERRORFILE # Error message appended to $ERRORFILE. bad_command3 # Error message echoed to stderr, #+ and does not appear in $ERRORFILE.
#=======================================================================
2>&1
filename 2>&1 bad_command >>filename 2>&1 # Appends both stdout and stderr to the file "filename" ... 2>&1 | [command(s)] bad_command 2>&1 | awk '{print $5}' # found # Sends stderr through a pipe. # |& was added to Bash 4 as an abbreviation for 2>&1 |.
i>&j
&j
0< FILENAME < FILENAME
[j]<>filename
#+ and assign file descriptor "j" to it.
echo 1234567890 > File # Write string to "File". exec 3<> File # Open "File" and assign fd 3 to it. read -n 4 <&3 # Read only 4 characters. echo -n . >&3 # Write a decimal point there. exec 3>&- # Close fd 3. cat File # ==> 1234.67890
|
cat *.txt | sort | uniq > result-file
keywords
log redirection