Question

Why is int"$((++k))"=test considered as a command by bash? Example:

$ int1=test
$ int"$((++k))"=test
bash: int1=test: command not found
$ 

I know that I could use declare int"$((++k))"=test, but why doesn't int"$((++k))"=test work?

Was it helpful?

Solution

This behavior is specified in POSIX 2.10.2.7 on the Shell Command Language (emphasis mine):

If all the characters preceding '=' form a valid name (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 3.230, Name), the token ASSIGNMENT_WORD shall be returned. (Quoted characters cannot participate in forming a valid name.)

OTHER TIPS

You can use eval like below to solve your problem with variable generation:

eval int$((++k))=test

Note that what's sent to eval will be processed once by the shell.

Because the bash grammar requires so.

<letter> ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|
             A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z

<digit> ::= 0|1|2|3|4|5|6|7|8|9

<number> ::= <digit>
           | <number> <digit>

<word> ::= <letter>
         | <word> <letter>
         | <word> '_'
         | <word> <digit>

<word_list> ::= <word>
             |  <word_list> <word>

<assignment_word> ::= <word> '=' <word>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top