如果user.name包含空格,则在bash脚本中配置git

时间:2017-06-27 16:40:05

标签: git bash shell environment-variables sh

我正在编写一个脚本来设置环境,我希望能够使用新名称,email和github acc配置git。我使用选项-n -e -g传递的新值分别如下面的脚本所示。

不幸的是,如果user.name包含空格,$git_name变量仅指它的第一部分。有没有办法正确地做到这一点干净?

#!/bin/bash

git_github=`git config --global --get-all  user.github`
git_email=`git config --global --get-all  user.email`
git_name="$(git config --global --get-all user.name)" 
echo "Before:" $git_github $git_email $git_name

while getopts :g:e:n: option
do
    case "${option}"
    in
    g) git config --global user.github ${OPTARG};;
    e) git config --global user.email ${OPTARG};;
    n) git config --global user.name ${OPTARG};;
    esac
done

git_github=`git config --get user.github`
git_email=`git config --get user.email`
git_name=`git config --get-all user.name`
echo "After:" $git_github $git_email $git_name

1 个答案:

答案 0 :(得分:0)

该命令需要将名称封装到括号中,因此解决方案是:

git config --global user.name "\"${OPTARG}\""
相关问题