bash_profile 函数到 git grep 和替换

bash_profile function to git grep and replace

我想在我的 ~/.bash_profile 中创建一个函数,它将 git grep 并列出包含字符串的文件,然后用另一个

function git-replace() { eval git grep -l | xargs sed -i '' -e 's///g' ; }

但是,如果我 运行 函数 git-replace "Type1" "Type2" ,什么也不会发生。我在这里做错了什么?

有 2 个问题:

  • 不要使用邪恶eval
  • 如果要扩展一个变量,不要使用单引号而是双引号,所以:
git-replace() {
    git grep -l "" | xargs sed -i '' -e "s///g"
}

现代 shell.

中不需要 function 语句

在shell中了解如何正确引用,这非常重要:

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words