mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Updated SC3021 (markdown)
42
SC3021.md
42
SC3021.md
@@ -1,9 +1,41 @@
|
|||||||
## In POSIX sh, `>&` is undefined.
|
## In POSIX sh, >& filename (as opposed to >& fd) is undefined.
|
||||||
|
|
||||||
This warning was retracted after 0.8.0.
|
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.
|
||||||
|
|
||||||
There are two forms of this operator: `>& fd`, and `>& filename`. The former is POSIX, and copies stdout to the given file descriptor or fails if it's not an integer. The latter is a bash specific extension meaning `> filename 2>&1`. Bash will interpret `>& $var` as one or the other depending on whether the value is an integer.
|
### Problematic code:
|
||||||
|
|
||||||
ShellCheck 0.8.0 and below was unable to differentiate and unintentionally treated both as `>& filename`, causing an invalid warning when the value was an integer.
|
```sh
|
||||||
|
# Writing to a filename
|
||||||
|
mycommand >& log.txt
|
||||||
|
```
|
||||||
|
|
||||||
If your script uses `>&` with an integer to copy a file descriptor, please ignore this warning. If it uses it to write both stdout and stderr to a filename, like `&>`, rewrite it explicitly to `> file 2>&1`.
|
### Correct code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mycommand > log.txt 2>&1
|
||||||
|
```
|
||||||
|
|
||||||
|
The following construct is also fine, as it uses an integer file descriptor instead of a filename:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
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 issue #2520](https://github.com/koalaman/shellcheck/issues/2520)
|
||||||
|
* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc!
|
Reference in New Issue
Block a user