bash 无法在case语句bash中设置变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6675879/
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
unable to set variable in case statement bash
提问by upbeat.linux
I'm trying to set a variable based on a bunch of input conditions. Here's a small sample of the code:
我试图根据一堆输入条件设置一个变量。这是代码的一个小示例:
#!/bin/bash
INSTANCE_SIZE=""
case "" in
"micro")
$INSTANCE_SIZE="t1.micro"
;;
"small")
$INSTANCE_SIZE="m1.small"
;;
esac
echo $INSTANCE_SIZE
When I run the script with the -ex switch and specify the proper argument:
当我使用 -ex 开关运行脚本并指定正确的参数时:
+ case "" in
+ =m1.small
./provision: line 19: =m1.small: command not found
回答by Blagovest Buyukliev
You need to remove the $sign in the assignments - INSTANCE_SIZE="m1.small". With the dollar sign, $INSTANCE_SIZEgets substituted with its value and no assignment takes place - bash rather tries to execute the command that resulted from the interpolation.
您需要删除$作业中的符号 - INSTANCE_SIZE="m1.small"。使用美元符号,$INSTANCE_SIZE会被替换为它的值,并且不会发生赋值——bash 而是尝试执行由插值产生的命令。

