xargs 不使用我的“ls”别名

elb*_*rna 8 hp-ux xargs alias aix

在 AIX 上(但这也发生在 HP-UX 上),我的路径中有 GNU ls 并且它的别名也为ls.

当我使用 xargs 时,它使用标准的 Unixls而不是别名。

例如(flocate是一个查找搜索主题的确切路径的函数):

flocate mirrorvg | xargs ls -lh
ls: illegal option -- h
usage: ls [-1ACFHLNRSabcdefgiklmnopqrstuxEUX] [File...]

ls -lh /usr/sbin/mirrorvg
-r-xr-x--- 1 root system 37K apr  3  2014 /usr/sbin/mirrorvg*
Run Code Online (Sandbox Code Playgroud)

为什么 xargs 不使用ls别名?

dha*_*hag 9

命令xargs只能运行命令,不能运行别名。然而,GNU parallel 能够运行以下函数:

The command must be an executable, a script, a composed
command, or a function. If it is a function you need to export
-f the function first. An alias will, however, not work (see
why http://www.perlmonks.org/index.pl?node_id=484296).
Run Code Online (Sandbox Code Playgroud)

所以我会推荐:

  • 为 xargs 提供您要使用的 ls 版本的完整路径(或一个明确的名称,可能gls取决于它在您的系统上的安装方式),或者,如果您的 shell 允许,

  • 定义ls为函数(function ls { gls "$@"; }; export -f ls在 bash 中)并使用 GNU parallel 而不是 xargs(parallel -j1如果您想使用单个 CPU)。