Skip to content

Files

Latest commit

 

History

History
40 lines (28 loc) · 1.04 KB

SC2210.md

File metadata and controls

40 lines (28 loc) · 1.04 KB

Pattern: Redirecting to an integer in shell script

Issue: -

Description

You are redirecting to or from a filename that is an integer. For example, ls > file where file happens to be 3.

This is not likely to be intentional. The most common causes are:

  1. Trying to compare two numbers, as in x > 5. This should instead be [ "$x" -gt 5 ] or (( x > 5 )).
  2. Trying similarly to compare command output, as in grep -c foo file > 100 instead of [ "$(grep -c foo file)" -gt 100 ]
  3. Malformed FD operations, such as writing 1>2 instead of 1>&2.

Example of incorrect code:

if x > 5; then echo "true"; fi

or

foo > /dev/null 2>1

Example of correct code:

if (( x > 5 )); then echo "true"; fi

or

foo > /dev/null 2>&1

Exceptions

None. If you do want to create a file named 4, you can quote it to silence shellcheck and make it more clear to humans that it's not supposed to be taken numerically.

Further Reading