我有一个脚本。
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'变量。
可以这样做吗?我的意思是将数组索引设置为变量。
答案 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]}"