I have pip install -U
aliased as pi
. I want to run:
zargs ~/scripts/python/**/requirements.txt -- pi -r
Is there any way to do this?
I also tried this ugly alternative:
zargs ~/scripts/python/**/requirements.txt -- ${aliases[pi]} -r
But it said (eval):2: command not found: pip install -U
.
I imagined zargs should be able to do exactly these kinds of stuff, since it is a zsh builtin.
Best Answer
Aliases are
csh
's poor man's functions. The aliases are not really commands, they are more text substitutions.alias
es have their use in more advanced shells that do have functions, as hacking tools for cases where functions can't be used, like in things like:Or
Which wouldn't work with functions. But here, it's not one of those cases.
Doesn't define a
pi
command, it defines api
alias. Here, as it happens, upon expansion, that ends up being turned into the start of a simple command, but aliased are not expanded in all cases where a command is expected. In particular, they are not expanded within functions likezargs
here (well, they are, but at the time of the function definition, not invocation, that's were ourforever
orfail
aliases above can be useful). And they are expanded in some contexts where not appropriate (like inpi() { ...; }
).A global alias is not a solution, global aliases are still not commands, they'll still text substitution, but expanded in even more cases.
After a
Now, a
pi
word is expanded wherever it occurs. So for instance,echo pi
would outputpip install -U
.Here, if you wanted to define a
pi
command, you would use a function:That one would be invoked by
zargs
.With your
pi
simple alias, you could still do:That is, invoke
$IFS
-splitting on the definition of the alias. Or going a bit further:where
z
does the splitting while taking into account quoting (so for instanceecho "foo bar"
be split intoecho
and"foo bar"
instead ofecho
,"foo
andbar"
),e
to perform expansions (likeecho $(uname)
expanded toecho Linux
for instance),Q
to remove the quotes, which would give a better approximation in a few more cases.