在shell脚本中设置键值对

时间:2015-05-13 18:31:08

标签: bash shell scripting bash4

我有一个shell脚本来引导我的机器:https://github.com/pathikrit/mac-setup-script/blob/master/setup.sh

我有几行代码来设置git:

git config --global rerere.enabled true
git config --global branch.autosetuprebase always
git config --global credential.helper osxkeychain

我想将其提取到顶部的关联数组(字典/散列映射),并在单行代码中调用它。我怎么能在bash 4 +中做到这一点?

1 个答案:

答案 0 :(得分:2)

# Create the associative array
declare -A opts
opts[rerere.enabled]=true
opts[branch.autosetuprebase]=always
opts[credential.helper]=osxkeychain

# Use the associative array
for k in "${!opts[@]}"
do
    git config --global "$k" "${opts[$k]}"
done