bashrc 惰性替换

bashrc 惰性替换

如何获得~/.bashrc别名$()在运行时评估替换,而不是在执行 ~/.bashrc 时(打开终端时)?

我经常运行这个命令,所以我想为它添加一个别名:

svn diff -r $(svn info | grep ^Revision | awk {'print $2'}):HEAD $(svn info | grep ^URL | awk {'print $2'}) | colordiff

但是,当我将其添加~/.bashrc为别名时,我发现评估被硬编码为我打开终端时的值:

$ alias
alias svnbranch='svn diff -r 178184:HEAD svn+ssh://branches/t4252 | colordiff'

如果我打开终端,~则会出现错误:

svn: E155007: '/home/dotancohen' is not a working copy
svn: E155007: '/home/dotancohen' is not a working copy
svn: E155007: '/home/dotancohen' is not a working copy
svn: E155007: '/home/dotancohen' is not a working copy
$ alias
alias svnbranch='svn diff -r :HEAD  | colordiff'
$

我已经尝试过这两种别名的变体~/.bashrc,它们都具有相同的效果(正如我预期的那样):

alias svnbranch="svn diff -r $(svn info | grep ^Revision | awk {'print $2'}):HEAD $(svn info | grep ^URL | awk {'print $2'}) | colordiff"
alias svnbranch="svn diff -r `svn info | grep ^Revision | awk {'print $2'}`:HEAD `svn info | grep ^URL | awk {'print $2'}` | colordiff"

如何获取别名来在运行时~/.bashrc评估替换$()

另外,一个人如何谷歌搜索一下这种情况?我尝试用谷歌搜索“bashrc 替换”、“bashrc 惰性替换”和其他关键短语,但我没有找到任何我认为应该是一个足够常见的问题来查找信息的内容。

答案1

使用单引号抑制特殊字符的处理。你也可以反斜杠 $s。

对于复杂的命令,您可能最好在任何情况下使用函数,该函数不需要任何转义并且更易于阅读和编辑:

svnbranch() {
    svn diff -r $(svn info | grep ^Revision | awk {'print $2'}):HEAD $(svn info | grep ^URL | awk {'print $2'}) | colordiff
}

您可以在可以定义别名的任何地方定义函数。

相关内容