How to use an alias in xargs

Ubuntu uses zsh, now has alias

alias | grep sayhi
sayhi='echo hi'

sayhi foo
hi foo

However, I cannot use this aliasin xargsor bash, see below

➜  ~  echo foo | xargs -i sayhi {}
xargs: sayhi: No such file or directory
➜  ~  echo foo | awk '{print "sayhi "$0}'
sayhi foo
➜  ~  echo foo | awk '{print "sayhi "$0}'|bash
bash: line 1: sayhi: command not found

It seems I cannot indirectly use aliasthe command line.

So how could I use aliasin this situation?

+4
source share
1 answer

Leaving aside the fact that you are dealing with two different shells:

  • xargs can only call external utilities (executable files), so by definition it cannot call an alias (directly).

  • bash , ( Bash / ).

, , - script xargs/bash.

, :

  • script sayhi ( ); , :

    #!/bin/bash
    echo hi $@
    
  • : chmod +x sayhi - .

  • , $PATH.

script, , , .

+3

Source: https://habr.com/ru/post/1624893/


All Articles