重击,读取变量,里面有变量

时间:2019-06-11 20:56:21

标签: bash variables

我有一个脚本。

style_header[true]="background-color: rgb(230,250,230);"
style_header[false]="background-color: rgb(250,230,230);"

if COMMAND; then
    export=true
else
    export=false
fi

echo "${style_header[$export]}"

命令完成,所以export = true,但是它返回style_header [false]变量“ background-color:rgb(250,230,230);”。

background-color: rgb(250,230,230);

我需要退货。

background-color: rgb(230,250,230);

它可以使用数字0或1作为索引,但是我需要在其中包含'true'或'false'变量。

可以这样做吗?我的意思是将数组索引设置为变量。

1 个答案:

答案 0 :(得分:3)

使用declare -A style_array将其声明为关联数组。默认情况下,它假定为索引数组。

#!/bin/bash

declare -A style_header
style_header[true]="background-color: rgb(230,250,230);"
style_header[false]="background-color: rgb(250,230,230);"

if COMMAND; then
export=true
else
export=false
fi

echo "${style_header[$export]}"

DEMO