declare not a valid identifier bash

jim picture jim · May 17, 2015 · Viewed 8.6k times · Source

I have a problem with my a bash script. What I do is assign variables like this.

   for ((i=START;i<=END;i++)
declare var$i=$(something)
done

And it works, but now I have a problem with finnish characters like å, ä, ö. What declare says is something like this

bash:declare 'wörd' not a valid identifier

Though it works fine if I do it like this

declare var2=$(sömething)

I can convert the characters with sed but it's better to have them like always, so this is a last resort solution. So I would like to know how can I assign variables like

var$i

with the finnish characters. The wörd word is part of the output of my command 'something'. When there are two words or more only the word(s) that contain the character ö , ä and so on are not assigned to the variable, so if the output of the command was "something wörd" then the only thing that is being shown with echo is something.

Answer

chepner picture chepner · May 17, 2015

bash simply does not allow identifiers using characters other than A-Z, a-z, 0-9, and _. However, the error you describe is just a side effect of that limitation. declare is a command which takes a string argument that looks like an assignment statement, but the semantics are slightly different. Consider this command:

foo () {
    echo "something wörd"
}

In an assignment statement, the right-hand side does not undergo word splitting, so you don't have to quote the command substitution. The following works fine:

$ word3=$(foo)
$ echo "$word"
something wörd

With declare, however, the command substitution does undergo word splitting, so when you write

$ i=3
$ declare word$i=$(foo)

it's equivalent to the command

$ declare word3=something wörd

which passes two names for declare to create, word3 (which gets a value) and wörd (which is an invalid name). declare word3="something wörd" would work fine; the shell's word-splitting is already done by the time declare gets the argument.

With declare, then, you need to quote the command substitution (or the entire string) in order for the entire output to be treated as the value for the new variable.

$ i=3
$ declare "word$i=$(foo)"
$ echo "$word3"
something wörd