Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash:${$(mycommand)%suffix}不';无法从mycommand输出中修剪后缀_Bash_Shell_Git Bash - Fatal编程技术网

Bash:${$(mycommand)%suffix}不';无法从mycommand输出中修剪后缀

Bash:${$(mycommand)%suffix}不';无法从mycommand输出中修剪后缀,bash,shell,git-bash,Bash,Shell,Git Bash,我正在尝试将目录更改为git存储库的远程位置。我正在使用命令: cd ${$(git remote get-url origin)%.git} 该命令不起作用: bash: ${$(git remote get-url origin)%.git}: bad substitution 我不明白为什么这是不正确的。是因为使用了$()而不是变量名吗?如果是,为什么?如何正确执行此操作?您不能在bash中使用嵌套字符串替换 相反,您可以使用以下一行: cd $(git remote get-url

我正在尝试将目录更改为git存储库的远程位置。我正在使用命令:

cd ${$(git remote get-url origin)%.git}
该命令不起作用:

bash: ${$(git remote get-url origin)%.git}: bad substitution

我不明白为什么这是不正确的。是因为使用了
$()
而不是变量名吗?如果是,为什么?如何正确执行此操作?

您不能在
bash
中使用嵌套字符串替换

相反,您可以使用以下一行:

cd $(git remote get-url origin | sed 's/\.git$//')
不能在删除子字符串的参数扩展中使用命令替换。您需要两个步骤:

tmp=$(git remote get-url origin)
cd "${tmp%.git}"
这将消除不好的替代


它本身在上操作(包括由名称、位置参数等引用的shell变量)。因此,如果您使用
%.git
从右侧删除
.git
,则它必须包含在某个变量中,例如上面的
tmp
,然后参数扩展按预期工作。

我也喜欢这样。如果我在那种情况下,我可能会这样做。它是一个单行程序,但会产生一个不必要的子shell,更不用说外部工具而不是shell内置。这只是一个偏好问题,
sed
不是bash内置的,但在每个
*nix
系统上仍然可用,我只是说这是不必要的性能开销(至少对于后缀删除这样的简单任务是如此)。对于完整的管道(git+sed vs.git+bash repl),我看到了1-2ms的增益(5-6 vs.3-4ms)。在环路(x1000)中,持续200ms增益。如果我们只看替换(sed与bash),而不看git调用,那么区别是800ms与9ms。