Quote:
Originally Posted by
wisecracker
OK, I'll go with that BUT, and a big but, why does Shell Check echo "$(( #txt - 1 ))" NOT come up with a syntax error knowing from the various scenarios that it does?
Should there not be a warning saying so. Note that in the Shell Check example I was using the worst case sh . Tested on the others gave errors or incorrect results too.
On reading RudiC's post it looks as though it SHOULD work but obviously it doesn't, or I am misinterpreting it?
Anyhow thanks, but I will keep '$/${}' in all cases just to be on the safe side.
There is something in what RudiC quoted in post #2 that says that the string
${#variable_name} inside an arithmetic expansion will undergo parameter expansion resulting in a number that is the number of characters in the string to which
$variable_name expands. (I.e.,
All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal.)
There is nothing in what RudiC quoted in post #2 that says that the string
#variable_name inside an arithmetic expansion should have any meaning (and I don't know of any shells where it does).
There is something in what RudiC quoted in post #2 that says that the strings
$variable_name and
${variable_name} inside an arithmetic expansion will expand to the string assigned to that variable. (I.e.,
All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal.)
There is nothing in what RudiC quoted in post #2 that says that the string
variable_name inside an arithmetic expansion will expand to the string that has been assigned to that variable. But, in addition to what RudiC quoted, the latest version of the standard also states the following requirement for arithmetic expansions:
Quote:
If the shell variable x contains a value that forms a valid integer constant, optionally including a leading <plus-sign> or <hyphen-minus>, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.
and I don't know of any shell that performs arithmetic expansions that does not also provide this shortcut.
I make absolutely no comment as to why
shellcheck seems to think that
$((#variable_name)) and
$(( #variable_name - 1 )) are not errors. You would have to ask the developers or maintainers of that code why they do not flag these constructs as errors in arithmetic expressions in shell scripts.