Get the alias definition 

Newsgroups: comp.unix.shell
Date: 27 Sep 2001 11:37:16 -0300

Aliases help me in my daily life. One problem is that they can't be used as xargs parameters, i.e.

xargs some_alias_command rest of parameters

will not work. I'm trying to solve this generically by a wrapper function:

xargs `get_alias_def some_alias_command` rest of parameters

But so far I haven't find an elegant solution yet:

$ alias atest="echo 'aa bb'"
$ alias atest
alias atest='echo '\''aa bb'\'''
$ alias atest | cut -d= -f 2
'echo '\''aa bb'\'''
$ echo `!!`
echo `alias atest | cut -d= -f 2`
'echo '\''aa bb'\'''

See? the single quote is still there. How can I get:

$ echo 'echo '\''aa bb'\'''
echo 'aa bb'

Get the alias definition 

>  xargs `get_alias_def some_alias_command` rest of parameters

Personally, I don't recommend using aliases for anything but interactive use, and then only sparingly. Really, an aliases major benefit is the ability to override builtin behavior - for instance, redefining cd. Overuse of aliases can lead to confusion, whereas use of aliases in scripts can lead to indeterminate behavior. What you are trying to do you can only do if you first alias xargs:

$ mkdir test
$ cd test
$ >a;>b;>c
$ alias xx='ls -ld'
$ alias xargs='\xargs '
$ echo * | xargs xx
-rw-rw-rw-   1 dam        users            0 Sep 28 09:35 a
-rw-rw-rw-   1 dam        users            0 Sep 28 09:35 b
-rw-rw-rw-   1 dam        users            0 Sep 28 09:35 c

Note that the alias for xargs is self-referencing. You must escape the self-reference in some fashion, by either using a quoting syntax as with the backslash, as above, or by designating the full path name. Also note the alias ends with a space. Normally, alias substitution is only done on the first word in a statement. However, if that first word is an alias and the alias ends in a space, the next word is checked for aliases. If that alias ends with a space, the next word is checked and so on.

This can lead to some confusing behavior, particularly with aliases of cd.

$ cd /opt
$ ls -l
drwxr-xr-x           ...     applix
...
$ cd applix
pd[4]: /opt/applix/applix: not a directory

Dan Mercer

Get the alias definition 

$ alias xargs1="xargs -l1"
$ echo * | xargs1 xx
xargs: xx: No such file or directory
$ alias xargs1="xargs -l1 "
$ echo * | xargs1 xx
-rw-rw----    1 tong     tong          118 Sep 18 22:56 Changes
-rw-rw----    1 tong     tong           46 Sep 18 22:56 MANIFEST
...

Get the alias definition 

You could use such a function:

xargs_with_alias() {
  alias_name=$1
  shift
  eval expanded_alias="`alias $alias_name | cut -d = -f2-`"
  eval xargs "$expanded_alias" '"$@"'
}

Stephane

Get the alias definition 

> >   eval xargs "$expanded_alias" '"$@"'
>
> Anybody else can come up with more generic solutions that don't
> tie up with xargs? I just use xargs as an example to explain the
> situation... thanks

What do you call a generic solution?

Maybe

expand_alias xargs -n 1 '[|' alias_name '|]' foo bar
(every argument between '[|' and '|]' is considered an alias and is
expanded):

expand_alias() {
  typeset -i n=1
  command=
  while (( n <= $# )); do
    if eval "[[ \$$n = '[|' \
              && $(( n + 2 )) -le $# \
              && \$$((n + 2)) = '|]' ]]"; then
      eval "alias_name=\$$((n + 1))"
      eval expanded_alias="`alias $alias_name | cut -d = -f2-`"
      command="$command $expanded_alias"
      n=n+3
    else
      command="$command \"\$$n\""
      n=n+1
    fi
  done
  eval "$command"
}

Not much tested.

Stephane

documented on: Tue 11-10-98 11:23:13