簡短版本: 爲什麼shell腳本會在其生成的字符串中產生轉義字符()嵌入(但不可見)?Shell腳本產生逃逸字符()與逃脫雙重qoutes
詳細版本:
我有產生Terraform命令並執行它的腳本。命令生成涉及使用變量來取代如下計算值:
...
AWS_VM_NUMBER=1
types="{"
array_limit=$((AWS_VM_NUMBER - 1))
for ((i = 0; i <= $array_limit; i++));
do
var=AWS_INSTANCE_TYPE_${i+1}
types=${types}"\"$i\"=\"${!var}\","
done
instance_types_array=${types%,}"}"
echo $instance_types_array ## prints {"0"="t2.small"} which is the correct fromat
...
# Now, executing a terraform command like this (please ignore the other variables as they exist else where in the code)
terraform apply -var \'access_key=$AWS_ACCESS_KEY\' \
-var \'secret_key=$AWS_SECRET_KEY\' \
-var \'instance_count=$AWS_VM_NUMBER\' \
-var \'region=$AWS_REGION\' \
-var \'instance_types=$instance_types_array\'
當執行terraform命令時,得到下面的錯誤:
invalid value "'instance_types={\"0\"=\"t2.small\"}'" for flag -var: Cannot parse value for variable ("{\"0\"=\"t2.small\"}'") as valid HCL: At 1:21: illegal char
如果我把該命令在一個變量和回聲它,它打印沒有反斜槓。但是,如果我複製它並在終端中手動執行它,我會得到相同的錯誤。它只會在我刪除雙引號並手工輸入時才起作用!
是的,謝謝:) – Sami