Skip to content
Joachim Ansorg edited this page Oct 31, 2022 · 5 revisions

In POSIX sh, >& filename (as opposed to >& fd) is undefined.

Note: ShellCheck 0.8.0 and below would trigger this warning on the perfectly POSIX compatible construct >& fd. If you are using >& to copy an integer file descriptor, please ignore this warning.

Problematic code:

# Writing to a filename
mycommand >& log.txt

Correct code:

mycommand > log.txt 2>&1

The following construct is also fine, as it uses an integer file descriptor instead of a filename:

fd=3
mycommand >& $fd

Rationale:

There are two forms of the >& file descriptor operator:

  • >& integer such as >& 3 is a POSIX compatible synonym for 1>&3
  • >& filename such as >& log.txt is a Bash specific synonym for > log.txt 2>&1

If (and only if) you are using the latter form, write it out as shown in the correct example to ensure portability.

Exceptions:

ShellCheck 0.8.0 and below incorrectly emits this warning for constructs like >& $var, even though this would be POSIX compatible when $var is an integer. In such cases, please ignore this warning.

Related resources:

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally