Shellcheck: SC2032 and SC2033 false positives when conditionally defining function

Created on 3 May 2018  路  3Comments  路  Source: koalaman/shellcheck

For bugs

  • Rule Id (if any, e.g. SC1000): SC2032/SC2033
  • My shellcheck version (shellcheck --version or "online"): 0.4.7
  • [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

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

#!/usr/bin/env bash

if [ "$( sudo -v )" ]; then
  sudo mv terraform /usr/bin/terraform
else
  terraform() { "${TRAVIS_BUILD_DIR}/terraform" "$@"; }
  export -f terraform
fi

Here's what shellcheck currently says:

Line 4:
  sudo mv terraform /usr/bin/terraform
          ^-- SC2033: Shell functions can't be passed to external commands.

Line 6:
  terraform() { "${TRAVIS_BUILD_DIR}/terraform" "$@"; }
  ^-- SC2032: Use own script or sh -c '..' to run this from sudo.

Here's what I wanted or expected to see:

No issues detected!

Most helpful comment

You're right, false positive. I think this is a result of the simple variable analysis shellcheck has. It figures that terraform was defined as a function, and used in sudo, even though they are in separate branches. I think you'ld have to ignore this unfortunately. I'm thinking of making the data flow analysis better, but that will take some time.

All 3 comments

I'm open to the fact that I might be doing something wrong, but I think this is a false positive. I'm conditionally creating the function, so it shouldn't even exist at the point I'm trying to run sudo instead.

I also realise this might be very hard to detect, and I will use a directive for now.

I think if you or some calling script wrap your whole code in brackets it will create the function and export it then check the condition so I think it's a valid warning. You should probably just re-name your function.

You're right, false positive. I think this is a result of the simple variable analysis shellcheck has. It figures that terraform was defined as a function, and used in sudo, even though they are in separate branches. I think you'ld have to ignore this unfortunately. I'm thinking of making the data flow analysis better, but that will take some time.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sobolevn picture sobolevn  路  4Comments

phagara picture phagara  路  4Comments

bje- picture bje-  路  3Comments

hugovk picture hugovk  路  4Comments

szepeviktor picture szepeviktor  路  4Comments