Pattern: Declaration modifier used as command name
Issue: -
In most languages, declaration modifiers like public
/static
/const
are keywords and you can apply multiple to any declaration.
In shell scripting they are instead command names, and anything after them is an argument. This means that readonly local foo
will create two readonly variables: local
, and foo
. Neither will be local.
Instead, either use multiple commands, or use a single declare
command with appropriate flags (declare
will automatically make a variable local when invoked in a function, unless -g
is passed to explicitly make it global).
Example of incorrect code:
local readonly foo=3
readonly export bar=4
Example of correct code:
local foo=3
readonly foo
readonly bar=4
export bar