shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

False-positive warnings about reserved word `time` when used as a function name

Open finefoot opened this issue 2 years ago • 0 comments

For bugs

  • Rule Id (if any, e.g. SC1000): SC1009, SC1036, SC1073, SC1072
  • My shellcheck version (shellcheck --version or 'online'): online
  • [x] I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • [ ] It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

  • [x] shellcheck.net (i.e. the latest commit) currently gives no useful warnings about this
  • [ ] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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

#!/bin/sh
time() {
  awk 'BEGIN{print srand(srand())}'
}
time

Here's what shellcheck currently says:

Line 2	SC1009: The mentioned syntax error was in this simple command.
Line 2	SC1036: '(' is invalid here. Did you forget to escape it?
Line 2	SC1073: Couldn't parse this explicit subshell. Fix to allow more checks.
Line 2	SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

Here's what I wanted or expected to see:

While time[1] is a reserved word[2] in Bash, I'm using #!/bin/sh here. In POSIX shell, time isn't a shell built-in but a utility[3] (which oftentimes isn't even shipped with modern Linux distributions). So this warning seems like a false-positive to me.

In other words, when testing above example locally, it doesn't work with #/bin/bash, but it does work just fine with #/bin/sh.


[1]https://www.gnu.org/software/bash/manual/html_node/Pipelines.html#index-time [2]https://www.gnu.org/software/bash/manual/html_node/Reserved-Words.html [3]https://pubs.opengroup.org/onlinepubs/9699919799/utilities/time.html

finefoot avatar May 19 '22 15:05 finefoot