shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

style: warn about using "shell keywords" or "builtins"

Open bittorf opened this issue 10 years ago • 5 comments

like warning about using keyword 'do' without quotation marks e.g.

x() {
  local arg="$1"
  echo "$arg"
}
x do
x "do"
x if
x "if"

keywords are e.g.:

user@bashbox:~$ compgen -k
if
then
else
elif
fi
case
esac
for
select
while
until
do
done
in
function
time
{
}
!
[[
]]
coproc

bittorf avatar Oct 26 '15 08:10 bittorf

I'm just another user of ShellCheck, but I don't see what the problem is. At least in Bash, pasting that example code into a new script echoes "do" and "if" twice each, exactly as expected. Does it behave differently in another shell?

Edit: I see an inconsistency when I run ShellCheck on this. It gives a warning for "Use semicolon or linefeed before 'do' (or quote to make it literal)." but doesn't say anything about "if". Although I don't think a keyword should necessarily be checked when used in the context of an argument to a command/function, any such checking should at least be consistent across keywords.

refola avatar Dec 19 '15 03:12 refola

OP here: ofcourse the code works, but IMHO it's bad style, e.g. it breaks the syntax highlightning and should just be avoided.

bittorf avatar Dec 19 '15 14:12 bittorf

ShellCheck currently warns about some mid- and end tokens like do/done/fi/esac because it's not at all obvious that the "then" and "fi" are literal in if true then true fi

There's no warning for start tokens, since it's more obvious that echo if true isn't a proper if statement.

I definitely agree that it's bad style, and there should be an optional warning about it.

koalaman avatar Dec 19 '15 22:12 koalaman

IMHO it's bad style

In mine, its fine

it breaks the syntax highlightning

Then your syntax highlighter is incorrect. There is no "the" syntax highlighter.

Why is this issue open after 7 years?

sboyd-m avatar Jan 31 '22 18:01 sboyd-m

Why is this issue open after 7 years?

I would assume it's because @koalaman stated "I definitely agree that it's bad style, and there should be an optional warning about it." and no one have yet fixed that?

brother avatar Mar 10 '22 15:03 brother