gcb*_*gcb 3 bash alias arguments
我的 bashrc 中通常有以下简单别名:
alias g="grep --color=always --exclude-dir=\*.svn\*"
但现在我必须在只有 GNU grep 2.5 的系统上工作,因此没有--exclude-dir争论。
现在我需要这样的工作:
alias g="grep --color=always $1 $2 | grep -v .svn"
但当然,参数会附加到别名的末尾。$1 和 $2 在创建别名时解析,而不是在调用时解析,即使使用强引号也是如此。
我可以在不求助于额外的脚本或函数的情况下解决这个问题吗?
不,你不能。正如 bash 手册所述:
在替换文本中没有使用参数的机制,如在 `csh' 中。如果需要参数,则应使用 shell 函数 (*note Shell Functions::)。
函数是正确的,并且可以像别名一样容易编写。在这种情况下,它可能是
function g () { grep --color=always "$1" "$2" | grep -v .svn ; }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2242 次 |
| 最近记录: |