x32x01
  • by x32x01 ||
List of All Bash Redirections - Linux Cheat-Sheet
List of All Bash Redirections - Linux Cheatsheet
Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection allows commands’ file handles to be duplicated, opened, closed, made to refer to different files, and can change the files the command reads from and writes to. Redirection may also be used to modify file handles in the current shell execution environment. The following redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.

Also Read: Bash CTRL and ALT Key Shortcuts

Each redirection that may be preceded by a file descriptor number may instead be preceded by a word of the form {varname}. In this case, for each redirection operator except >&- and <&-, the shell will allocate a file descriptor greater than 10 and assign it to {varname}. If >&- or <&- is preceded by {varname}, the value of varname defines the file descriptor to close.

cmd > fileRedirect the standard output (stdout) of cmd to a file.
cmd 1> fileSame as cmd > file. 1 is the default file descriptor (fd) for stdout.
cmd 2> fileRedirect the standard error (stderr) of cmd to a file. 2 is the default fd for stderr.
cmd >> fileAppend stdout of cmd to a file.
cmd 2>> fileAppend stderr of cmd to a file.
cmd &> fileRedirect stdout and stderr of cmd to a file.
cmd > file 2>&1Another way to redirect both stdout and stderr of cmd to a file. This is not the same as cmd 2>&1 > file.
cmd > /dev/nullDiscard stdout of cmd.
cmd 2> /dev/nullDiscard stderr of cmd.
cmd &> /dev/nullDiscard stdout and stderr of cmd.
cmd < fileRedirect the contents of the file to the standard input (stdin) of cmd.
cmd << EOL
line1
line2
EOL
Redirect a bunch of lines to the stdin. If ‘EOL’ is quoted, text is treated literally. This is called a here-document.
cmd <<- EOL
<tab>foo
<tab>bar
EOL
Redirect a bunch of lines to the stdin and strip the leading tabs.
cmd <<< “string”Redirect a single line of text to the stdin of cmd. This is called a here-string.
exec 2> fileRedirect stderr of all commands to a file forever.
exec 3< fileOpen a file for reading using a custom file descriptor.
exec 3> fileOpen a file for writing using a custom file descriptor.
exec 3<> fileOpen a file for reading and writing using a custom file descriptor.
exec 3>&-Close a file descriptor.
exec 4>&3Make file descriptor 4 to be a copy of file descriptor 3. (Copy fd 3 to 4.)
exec 4>&3-Copy file descriptor 3 to 4 and close file descriptor 3.
echo “foo” >&3Write to a custom file descriptor.
cat <&3Read from a custom file descriptor.
(cmd1; cmd2) > fileRedirect stdout from multiple commands to a file (using a sub-shell).
{ cmd1; cmd2; } > fileRedirect stdout from multiple commands to a file (faster; not using a sub-shell).
exec 3<> /dev/tcp/host/portOpen a TCP connection to host: port. (This is a bash feature, not Linux feature).
exec 3<> /dev/udp/host/portOpen a UDP connection to host: port. (This is a bash feature, not Linux feature).
cmd <(cmd1)Redirect stdout of cmd1 to an anonymous fifo, then pass the fifo to cmd as an argument. Useful when cmd doesn’t read from stdin directly.
cmd < <(cmd1)Redirect stdout of cmd1 to an anonymous fifo, then redirect the fifo to stdin of cmd. Best example: diff <(find /path1 | sort) <(find /path2 | sort).
cmd <(cmd1) <(cmd2)Redirect stdout of cmd1 and cmd2 to two anonymous fifos, then pass both fifos as arguments to cmd.
cmd1 >(cmd2)Run cmd2 with its stdin connected to an anonymous fifo, and pass the filename of the pipe as an argument to cmd1.
cmd1 > >(cmd2)Run cmd2 with its stdin connected to an anonymous fifo, then redirect stdout of cmd to this anonymous pipe.
cmd1 | cmd2Redirect stdout of cmd1 to stdin of cmd2.
cmd1 |& cmd2Redirect stdout and stderr of cmd1 to stdin of cmd2 (bash 4.0+ only). Use cmd1 2>&1 | cmd2 for older bashes.
cmd | tee fileRedirect stdout of cmd to a file and print it to screen.
exec {filew}> fileOpen a file for writing using a named file descriptor called {filew} (bash 4.1+).
cmd 3>&1 1>&2 2>&3Swap stdout and stderr of cmd.
cmd > >(cmd1) 2> >(cmd2)Send stdout of cmd to cmd1 and stderr of cmd to cmd2.
cmd1 | cmd2 | cmd3 | cmd4
echo ${PIPESTATUS[@]}
Find out the exit codes of all piped commands.
 

Similar Threads

x32x01
  • x32x01
Replies
0
Views
218
x32x01
x32x01
x32x01
Replies
0
Views
102
x32x01
x32x01
x32x01
Replies
0
Views
134
x32x01
x32x01
x32x01
Replies
0
Views
122
x32x01
x32x01
x32x01
Replies
0
Views
102
x32x01
x32x01
TAGs: Tags
linux cheat-sheet

Register & Login Faster

Forgot your password?

Latest Resources

Forum Statistics

Threads
517
Messages
518
Members
45
Latest Member
Tacola
Back
Top