【问题标题】:Configure git in bash script if user.name contains spaces如果 user.name 包含空格,则在 bash 脚本中配置 git
【发布时间】:2017-11-30 19:46:07
【问题描述】:

我正在编写一个脚本来设置环境,我希望能够使用新名称、电子邮件和 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

【问题讨论】:

    标签: git bash shell environment-variables sh


    【解决方案1】:

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

    git config --global user.name "\"${OPTARG}\""
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2023-01-27
      • 2015-02-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多