如何在xargs中使用别名命令? [英] How can I use aliased commands with xargs?
问题描述
我的.aliases中具有以下别名:
I have the following alias in my .aliases:
alias gi grep -i
,我想在名称中带有字符串bar
的所有文件中不区分大小写地查找foo
:
and I want to look for foo
case-insensitively in all the files that have the string bar
in their name:
find -name \*bar\* | xargs gi foo
这就是我得到的:
xargs: gi: No such file or directory
是否可以在xargs中使用别名,还是必须使用完整版本:
Is there any way to use aliases in xargs, or do I have to use the full version:
find -name \*bar\* | xargs grep -i foo
注意:这是一个简单的示例.除了gi
之外,我还有一些非常复杂的别名,无法轻易手动扩展.
Note: This is a simple example. Besides gi
I have some pretty complicated aliases that I can't expand manually so easily.
:我使用的是tcsh
,所以请指定答案是否特定于外壳.
I used tcsh
, so please specify if an answer is shell-specific.
推荐答案
别名是特定于shell的-在这种情况下,很可能是特定于bash的.要执行别名,您需要执行bash,但是别名仅针对交互式shell加载(更精确地,.bashrc
仅针对交互式shell读取).
Aliases are shell-specific - in this case, most likely bash-specific. To execute an alias, you need to execute bash, but aliases are only loaded for interactive shells (more precisely, .bashrc
will only be read for an interactive shell).
bash -i 运行一个交互式外壳程序(以及源.bashrc). bash -c cmd 运行 cmd .
bash -i runs an interactive shell (and sources .bashrc). bash -c cmd runs cmd.
将它们放在一起:
bash -ic cmd 在交互式外壳中运行 cmd ,其中 cmd 可以是在您的.bashrc
.
Put them together:
bash -ic cmd runs cmd in an interactive shell, where cmd can be a bash function/alias defined in your .bashrc
.
find -name \*bar\* | xargs bash -ic gi foo
应该做你想做的事.
我看到您已经将此问题标记为"tcsh",因此bash特定的解决方案不适用.使用tcsh,您不需要-i
,因为它看起来像读.tcshrc,除非您输入-f
.
I see you've tagged the question as "tcsh", so the bash-specific solution is not applicable. With tcsh, you dont need the -i
, as it appears to read .tcshrc unless you give -f
.
尝试一下:
find -name \*bar\* | xargs tcsh -c gi foo
它适用于我的基本测试.
It worked for my basic testing.
这篇关于如何在xargs中使用别名命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!