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.

jim
  • 63
  • 1
  • 2
  • 6
  • 1
    How do Finnish characters show up in `$i`, and why do you want to do this? – Ry- May 17 '15 at 17:44
  • 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. – jim May 17 '15 at 17:50
  • 1
    Where does `wörd` come in? – Ry- May 17 '15 at 17:54
  • Take a look at http://stackoverflow.com/a/2821201/3776858 – Cyrus May 17 '15 at 17:55
  • 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. – jim May 17 '15 at 17:56
  • 1
    `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. – chepner May 17 '15 at 19:04

2 Answers2

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
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    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. – rici May 17 '15 at 22:13
  • 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. – chepner May 17 '15 at 22:19
  • 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.) – rici May 17 '15 at 22:28
  • 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`. – John Von Neumann Jul 29 '19 at 07:03
0

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

Instead of :

>> sh script.sh 

Try :

>> /bin/bash script.sh 
Walk
  • 1,531
  • 17
  • 21