Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if $variable; then should throw an error because it does not do what many inexperienced people expect it to do #2945

Open
2 tasks done
artificial-intelligence opened this issue Mar 14, 2024 · 0 comments

Comments

@artificial-intelligence

For new checks and feature suggestions

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

#!/bin/bash
is_true="yes"
if $is_true; then
echo "variable is $is_true";
fi

Here's what shellcheck currently says:

$ shellcheck myscript
No issues detected!

Here's what I wanted or expected to see:

Line 3:
if $is_true
  ^-- [SCFOOBAR](https://www.shellcheck.net/wiki/SCFOOBAR) (warning): Did you mean to test the content of a variable? use [[ "$is_true" == "foobar" ]] instead of `$is_true`. If you really want to run a command make sure it terminates.

This seems to be a common error made by python programmers who are not familiar with bash, to just check if the variable is set or they expect that a truth value is automatically carried over.

what instead happens, if a command like "yes" is the content of the variable, it get's executed indefinitely.

I think this can't be an error, because there is the legitimate usecase of putting commands in variables and then executing them - a common pattern is e.g. to construct a commandline via different variables which are concatenated together given different conditions.

But I think this at least should be an optional warning.

It's an ambiguous issue for sure.

Thanks for your consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant