Shellcheck: SC2002: Another useful use of cat

Created on 13 Jan 2020  路  1Comment  路  Source: koalaman/shellcheck

For bugs

  • Rule Id: SC2002
  • My shellcheck version: 0.7.0
  • [v] The rule's wiki page does not already cover this
  • [v] I tried on shellcheck.net and verified that this is still a problem on the latest commit

Here's a snippet or screenshot that shows the problem:

#!/usr/bin/env bash

cat "${1--}" | lolcat
#lolcat < "${1--}"  #this would fail, when no argument is supplied

Here's what shellcheck currently says:

Line 3:
cat "${1--}" | lolcat
^-- SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.

Here's what I wanted or expected to see:

When cat's argument expands to a dash ("-"), cat will use the stdin instead of the file. In the snippet this means that when no argument is supplied, the stdin is fed to lolcat.

With lolcat< "${1--}", bash raises the error "No such file or directory" when $1 is unset.

Not sure how shellcheck should handle this. Suggestion: perhaps don't show this error only when a variable is referenced using ${x--}? So if the programmer wants to use stdin instead of a file, the variable $x has to be unset. This will add one extra line to translate "-" to an unset variable.

Most helpful comment

Not a useful use of cat. When lolcat's argument expands to a dash, lolcat will also use stdin instead of the file.

#!/usr/bin/env bash
# Both lines below work
lolcat "${1--}"
lolcat < "${1-/dev/stdin}" # If you want to use redirection

>All comments

Not a useful use of cat. When lolcat's argument expands to a dash, lolcat will also use stdin instead of the file.

#!/usr/bin/env bash
# Both lines below work
lolcat "${1--}"
lolcat < "${1-/dev/stdin}" # If you want to use redirection
Was this page helpful?
0 / 5 - 0 ratings