declare not a valid identifier bash
Asked Answered
G

2

6

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.

Gluttonize answered 17/5, 2015 at 17:41 Comment(6)
How do Finnish characters show up in $i, and why do you want to do this?Sterilize
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.Gluttonize
Where does wörd come in?Sterilize
Take a look at https://mcmap.net/q/41109/-allowed-characters-in-linux-environment-variable-namesCurnin
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.Gluttonize
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.Britteny
B
6

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
Britteny answered 17/5, 2015 at 19:25 Comment(4)
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.Vanover
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.Britteny
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.)Vanover
To actually take what @Vanover said one further, the bash identifier issue is a situation that can arise when you utilise shell opts, particularly set -o posix.Correggio
T
0

Try running your script using /bin/bash in command line instead if sh (:

Instead of :

>> sh script.sh 

Try :

>> /bin/bash script.sh 
Toratorah answered 18/5, 2017 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.