declare not a valid identifier bash
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
jim
Updated on June 20, 2022Comments
-
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- almost 9 yearsHow do Finnish characters show up in
$i
, and why do you want to do this? -
jim almost 9 yearsI 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- almost 9 yearsWhere does
wörd
come in? -
Cyrus almost 9 yearsTake a look at stackoverflow.com/a/2821201/3776858
-
jim almost 9 yearsThe 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 almost 9 years
bash
identifiers are ASCII only (a-z, A-Z, 0-9, _). A barewörd=1
(withoutdeclare
) 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 almost 9 yearsThe 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 whichisalpha
in the current locale, and to continue with any character isisalnum
. 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 almost 9 yearsThanks. 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 almost 9 yearsIt'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 over 4 yearsTo 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
.