Incorrect Redirection Order Medium

The order of redirection for 'stdout' and 'stderr' is not correct. This can lead to unexpected behavior, as the shell may redirect the outputs in a way that is different from the intended behavior. To avoid this, the '2>&1' redirection must be placed at the end of the command.

Detector ID
shell/incorrect-redirection-order@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: stderr redirected to original stdout, then stdout redirected to file.
3ls -l /nonexistent 2>&1 > output.txt

Compliant example

1
2# Compliant: stdout redirected to file, then stderr redirected to new stdout (file).
3ls -l /nonexistent > output.txt 2>&1