我如何 运行 这个 bash 脚本需要同时添加两个注释

How can I run this bash script Need to add two comment together

alias git-repo="xdg-open" + "git config --get remote.origin.url | sed -e 's/:/\//g'| sed -e 's/ssh\/\/\///g'| sed -e 's/git@/https:\/\//g'"

我想在浏览器中打开 link 我可以通过此脚本的第二部分获得 link 但我只需要添加 xdg-open 作为前缀

您可以在别名中使用由 ; 分隔的两个命令:

alias e="echo 1; echo 2"
e
1
2

但您似乎想使用 git config 的输出作为 xdh-open 的输入。在这种情况下,您需要使用 $(...)。将三个 sed 命令合并为一个得到:

alias git-repo="xdg-open $(git config --get remote.origin.url | sed -e 's/:/\//g' -e 's/ssh\/\/\///g' -e 's/git@/https:\/\//g')"

但是如果您这样定义别名,git config 会在您定义别名时运行,您可能希望在实际使用别名时发生这种情况。所以把它放在单引号里:

alias git-repo="xdg-open '$(git config --get remote.origin.url | sed -e 's/:/\//g' -e 's/ssh\/\/\///g' -e 's/git@/https:\/\//g')'"

正如@KamilCuk 在评论中所说,如果变得更复杂,shell 脚本或函数可能会更好:

function git_repo {
    url=$(git config --get remote.origin.url | sed -e 's/:/\//g' -e 's/ssh\/\/\///g' -e 's/git@/https:\/\//g')
    echo "$url"
    xdg-open "$url"
}

您可以将函数放在例如 ~/.bashrc 或您 source.

的任何其他文件中

作为脚本:

set -eu
url=$(git config --get remote.origin.url | sed -e 's/:/\//g' -e 's/ssh\/\/\///g' -e 's/git@/https:\/\//g')
echo "$url"
xdg-open "$url"

这更易于阅读和使用,您的编辑器可以突出显示语法,shellcheck 可以在出现问题时警告您。