Getting started
Using trap to react to signals and system events
Listing files
Aliasing
Jobs and processes
Redirection
Control structures
Using cat
Arrays
Functions
Bash Parameter Expansion
Sourcing
Find
Here documents and here strings
Quoting
Conditional Expressions
Scripting with parameters
History substitutions
Math
Scoping
Process substitution
Programmable completion
Customizing PS1
Brace Expansion
Bash Arithmetic
getopts smart positional-parameter parsing
Debugging
Pitfalls
Script shebang
Pattern matching and regular expressions
Keyboard shortcuts
Change shell
Copying with cp
Internal variables
Job control
Case statement
Word splitting
Read a file
File transfer with scp
Pipelines
Managing PATH environment variable
Avoiding date using printf
Chain of commands and operations
Type of shells
true, false and commands
Color script output cross-platform
Navigating directories
Using sort
Namespace
co-processes
Typing variables
Jobs at specific types
Associative arrays
Handling the system prompt
Creating directories
File execution sequence
The cut command
Bash on Windows 10
Cut command
Splitting files
Global and local variables
Design Patterns
CGI Scripts
Select keyword
When to use eval
Networking with bash
Parallel
Grep
Strace
Sleep
Decodi
Contributors

Redirection

suggest change

Syntax

Parameters

Parameter | Details |

——— | —–– | internal file descriptor | An integer. |

direction | One of \>, \< or <> | external file descriptor or path | & followed by an integer for file descriptor or a path. |

Remarks

UNIX console programs have an input file and two output files (input and output streams, as well as devices, are treated as files by the OS.) These are typically the keyboard and screen, respectively, but any or all of them can be redirected to come from — or go to — a file or other program.

STDIN is standard input, and is how the program receives interactive input. STDIN is usually assigned file descriptor 0.

STDOUT is standard output. Whatever is emitted on STDOUT is considered the “result” of the program. STDOUT is usually assigned file descriptor 1.

STDERR is where error messages are displayed. Typically, when running a program from the console, STDERR is output on the screen and is indistinguishable from STDOUT. STDERR is usually assigned file descriptor 2.

The order of redirection is important

command > file 2>&1

Redirects both (STDOUT and STDERR) to the file.

command 2>&1 > file

Redirects only STDOUT, because the file descriptor 2 is redirected to the file pointed to by file descriptor 1 (which is not the file file yet when the statement is evaluated).

Each command in a pipeline has its own STDERR (and STDOUT) because each is a new process. This can create surprising results if you expect a redirect to affect the entire pipeline. For example this command (wrapped for legibility):

$ python -c 'import sys;print >> sys.stderr, "Python error!"' \
| cut -f1 2>> error.log

will print “Python error!” to the console rather than the log file. Instead, attach the error to the command you want to capture:

$ python -c 'import sys;print >> sys.stderr, "Python error!"' 2>> error.log \
| cut -f1

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents