声明不是有效的标识符 bash
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30289988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
declare not a valid identifier bash
提问by jim
I have a problem with my a bash script. What I do is assign variables like this.
我的 bash 脚本有问题。我所做的是分配这样的变量。
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
我可以使用 sed 转换字符,但最好像往常一样使用它们,所以这是最后的解决方案。所以我想知道如何分配变量,例如
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.
带有芬兰语字符。w?rd 字是我的命令'something' 输出的一部分。当有两个或更多单词时,只有包含字符的单词?, ? 等等都没有分配给变量,所以如果命令的输出是“something w?rd”,那么用 echo 显示的唯一东西就是东西。
回答by chepner
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 lookslike an assignment statement, but the semantics are slightly different. Consider this command:
bash
根本不允许使用 AZ、az、0-9 和 _ 以外的字符的标识符。但是,您描述的错误只是该限制的副作用。declare
是一个带有字符串参数的命令,它看起来像一个赋值语句,但语义略有不同。考虑这个命令:
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 doesundergo word splitting, so when you write
declare
但是,使用,命令替换确实会进行分词,因此当您编写
$ i=3
$ declare word$i=$(foo)
it's equivalent to the command
它相当于命令
$ declare word3=something w?rd
which passes twonames 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.
它传递两个名称declare
来创建,word3
(获取一个值)和w?rd
(这是一个无效的名称)。declare word3="something w?rd"
会工作得很好;shell 的分词在declare
获取参数时已经完成。
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.
使用declare
,您需要引用命令替换(或整个字符串),以便将整个输出视为新变量的值。
$ i=3
$ declare "word$i=$(foo)"
$ echo "$word3"
something w?rd
回答by Walk
Try running your script using /bin/bash
in command line instead if sh
(:
尝试/bin/bash
在命令行中使用if sh
(:
Instead of :
代替 :
>> sh script.sh
Try :
尝试 :
>> /bin/bash script.sh