Skip to main content
added 23 characters in body
Source Link
ilkkachu
  • 148.1k
  • 16
  • 268
  • 441
  • << : A here document. It is often used to print multi-line strings.

       command << WORD
           Text
       WORD
    

    Here, command will take everything until it finds the next occurrence of WORD, Text in the example above, as input . While WORD is often EoF or variations thereof, it can be any alphanumeric (and not only) string you like. When any part of WORD is quoted or escaped, the text in the here document is treated literally and no expansions are performed (on variables for example). If it is unquoted, variables will be expanded. For more details, see the bash manual.

    If you want to pipe the output of command << WORD ... WORD directly into another command or commands, you have to put the pipe on the same line as << WORD, you can't put it after the terminating WORD or on the line following. For example:

       command << WORD | command2 | command3...
           Text
       WORD
    
  • <<< : Here strings, similar to here documents, but intended for a single line. These exist only in the Unix port or rc (where it originated), zsh, some implementations of ksh, yash and bash.

      command <<< WORD
    
  • << : A here document. It is often used to print multi-line strings.

       command << WORD
           Text
       WORD
    

    Here, command will take everything until it finds the next occurrence of WORD, Text in the example above, as input . While WORD is often EoF or variations thereof, it can be any alphanumeric (and not only) string you like. When WORD is quoted, the text in the here document is treated literally and no expansions are performed (on variables for example). If it is unquoted, variables will be expanded. For more details, see the bash manual.

    If you want to pipe the output of command << WORD ... WORD directly into another command or commands, you have to put the pipe on the same line as << WORD, you can't put it after the terminating WORD or on the line following. For example:

       command << WORD | command2 | command3...
           Text
       WORD
    
  • <<< : Here strings, similar to here documents, but intended for a single line. These exist only in the Unix port or rc (where it originated), zsh, some implementations of ksh, yash and bash.

      command <<< WORD
    
  • << : A here document. It is often used to print multi-line strings.

       command << WORD
           Text
       WORD
    

    Here, command will take everything until it finds the next occurrence of WORD, Text in the example above, as input . While WORD is often EoF or variations thereof, it can be any alphanumeric (and not only) string you like. When any part of WORD is quoted or escaped, the text in the here document is treated literally and no expansions are performed (on variables for example). If it is unquoted, variables will be expanded. For more details, see the bash manual.

    If you want to pipe the output of command << WORD ... WORD directly into another command or commands, you have to put the pipe on the same line as << WORD, you can't put it after the terminating WORD or on the line following. For example:

       command << WORD | command2 | command3...
           Text
       WORD
    
  • <<< : Here strings, similar to here documents, but intended for a single line. These exist only in the Unix port or rc (where it originated), zsh, some implementations of ksh, yash and bash.

      command <<< WORD
    
Rollback to Revision 18
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

POSIX definition

RedirectionPOSIX definition of Redirection Operator

  • >& : (per POSIX spec) when surrounded by digits (1>&2) or - on the right side (1>&-) either redirects only one file descriptor or closes it (>&-).

A >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.

If the right side of this redirection is a file please read the next entry.

  • &>, >&, &>, >>& and &>> : (non-standardread above also). Redirect both standard error and standard output, replacing or appending, respectively.

      command &> out.txt
    

The &> variant originates in bash, while the >& variant comes from csh (decades earlier). They both conflict with other POSIX shell operators and should not be used in portable sh scripts. (However >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.)

Redirection Operator

  • &>, >&, >>& and &>> : (non-standard). Redirect both standard error and standard output, replacing or appending, respectively.

      command &> out.txt
    

The &> variant originates in bash, while the >& variant comes from csh (decades earlier). They both conflict with other POSIX shell operators and should not be used in portable sh scripts. (However >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.)

POSIX definition

POSIX definition of Redirection Operator

  • >& : (per POSIX spec) when surrounded by digits (1>&2) or - on the right side (1>&-) either redirects only one file descriptor or closes it (>&-).

A >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.

If the right side of this redirection is a file please read the next entry.

  • >&, &>, >>& and &>> : (read above also) Redirect both standard error and standard output, replacing or appending, respectively.

      command &> out.txt
    

The &> variant originates in bash, while the >& variant comes from csh (decades earlier). They both conflict with other POSIX shell operators and should not be used in portable sh scripts.

Rollback to Revision 16
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

POSIX definition

POSIX definition of RedirectionRedirection Operator

  • >& : (per POSIX spec) when surrounded by digits (1>&2) or - on the right side (1>&-) either redirects only one file descriptor or closes it (>&-).

A >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.

If the right side of this redirection is a file please read the next entry.

  • >&, &>, >&, >>& and &>> : (read above alsonon-standard). Redirect both standard error and standard output, replacing or appending, respectively.

      command &> out.txt
    

The &> variant originates in bash, while the >& variant comes from csh (decades earlier). They both conflict with other POSIX shell operators and should not be used in portable sh scripts. (However >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.)

POSIX definition

POSIX definition of Redirection Operator

  • >& : (per POSIX spec) when surrounded by digits (1>&2) or - on the right side (1>&-) either redirects only one file descriptor or closes it (>&-).

A >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.

If the right side of this redirection is a file please read the next entry.

  • >&, &>, >>& and &>> : (read above also) Redirect both standard error and standard output, replacing or appending, respectively.

      command &> out.txt
    

The &> variant originates in bash, while the >& variant comes from csh (decades earlier). They both conflict with other POSIX shell operators and should not be used in portable sh scripts.

Redirection Operator

  • &>, >&, >>& and &>> : (non-standard). Redirect both standard error and standard output, replacing or appending, respectively.

      command &> out.txt
    

The &> variant originates in bash, while the >& variant comes from csh (decades earlier). They both conflict with other POSIX shell operators and should not be used in portable sh scripts. (However >& followed by a file descriptor number is a portable way to redirect a file descriptor, and >&- is a portable way to close a file descriptor.)

deleted 166 characters in body
Source Link
user232326
user232326
Loading
Extended description of `>&` and `&>` redirection.
Source Link
user232326
user232326
Loading
https://unix.stackexchange.com/questions/590694/posix-compliant-way-to-redirect-stdout-and-stderr-to-a-file
Source Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
Loading
Correct `!` description. Links to operators (redirection and control) reference in the posix spec.
Source Link
user232326
user232326
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Loading
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link
Loading
added info about piping from herenow docs
Source Link
cas
  • 84.9k
  • 9
  • 138
  • 207
Loading
added 203 characters in body
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
Loading
added 818 characters in body
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
Loading
There's no `>>(:)` just `>` redirection to a `>(...)` process substitution.
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
Loading
added 5 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading
corrected 2x "command1" where "command" should stand
Source Link
Kimmax
  • 101
  • 3
Loading
added 529 characters in body
Source Link
mikeserv
  • 59.4k
  • 10
  • 123
  • 244
Loading
added 164 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
Loading
indicate which operators are available only in some shells; group the sentence punctuation operators; link to my well-appreciated answer on brackets; show equivalences of && and ||; mention !; mention >|; mention process substitution
Source Link
Gilles 'SO- stop being evil'
  • 866.1k
  • 205
  • 1.8k
  • 2.3k
Loading
Post Made Community Wiki by terdon
added 3 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading