declare not a valid identifier bash

10,369

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
Share:
10,369
jim
Author by

jim

Updated on June 20, 2022

Comments

  • jim
    jim almost 2 years

    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.

    • Ry-
      Ry- almost 9 years
      How do Finnish characters show up in $i, and why do you want to do this?
    • jim
      jim almost 9 years
      I assign the output of some commands to var1,var2 and then do calculations. Sorry I don't knwo what you mean on the first part.
    • Ry-
      Ry- almost 9 years
      Where does wörd come in?
    • Cyrus
      Cyrus almost 9 years
    • jim
      jim almost 9 years
      The wörd comes is part of the output of the command is assign the variable to. Something like this: declare var$i=$(something) so the wörd is the output of the command something.
    • chepner
      chepner almost 9 years
      bash identifiers are ASCII only (a-z, A-Z, 0-9, _). A bare wörd=1 (without declare) is interpreted as a command, not an assignment statement, due to the non-ASCII character. It could deviate from the POSIX spec, as described in the link provided by Cyrus, but it doesn't.
  • rici
    rici almost 9 years
    The claim about bash identifiers being restricted to [A-Za-z_][A-Za-z0-9_]* is not true. Bash allows an identifier to start with any single-byte character which isalpha in the current locale, and to continue with any character is isalnum. If the locale were iso-8859-1, then scandinavian, french, portuguese and spanish alphabetic letters would work just fine. However, it won't work with UTF-8 locales because the non-us-ascii alphabetic characters are multibyte.
  • chepner
    chepner almost 9 years
    Thanks. I knew as I was typing that part of the answer it sounded unnecessarily strict, but I did t think to test it with a non-English locale.
  • rici
    rici almost 9 years
    It's not easy to test, either, since most systems these days use UTF-8 console I/O, even if the locale is changed. So you have to create an ISO-8859-1 file as well as setting the locale. But it does work, honest. (And Posix requires it to work, too.)
  • John Von Neumann
    John Von Neumann over 4 years
    To actually take what @rici said one further, the bash identifier issue is a situation that can arise when you utilise shell opts, particularly set -o posix.