Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: execute or print one-liner

Here is an idea:

# Helper function to define expandable functions
exec-or-expand() { if [[ -n $EXPAND ]]; then print -z "$@"; else "$@"; fi; }  

# Command to force the expansion of a function defined with exec-or-expand
expand() { EXPAND=1 "$@"; }

# Example of expandable function
f1() { exec-or-expand echo blahblah -o1 -o2 $1 -o3 $2 $3 }

% f1 a b c
blahblah -o1 -o2 a -o3 b c
% expand f1 a b c
% echo blahblah -o1 -o2 a -o3 b c
blahblah -o1 -o2 a -o3 b c

Caveat: probably won't do what's desired if redirections are used.


On Wed, Nov 30, 2022 at 3:10 PM Thomas Lauer <thomas.lauer@xxxxxxxxxx> wrote:
Hi folks,

I have dozens, if not hundreds, of one-liners, many as aliases and many
others as simple one-line functions (used whenever some parameters need
a little massaging before execution). That's working very well.

I have early on stumbled across a zsh function that would expand an
alias on the command line so that I can edit or add stuff and I use that
for many aliases. However, I just can't find a similar thing for
one-line functions, ie a function that will just print the one-line
function body to the command line but not execute it. Given

f1() { blahblah -o1 -o2 $1 -o3 $2 $3 }

executing f1() with three parameters is simple:

f1 a b c

What I'm looking for is a way to have f1's body injected into the
command line, ie something akin to

print -z blahblah -o1 -o2 -o3

($1-$3 will be empty as they are undefined.)

I can write a second function print-z-ing the required string but that
feels redundant... although I have no idea how to do this w/o such a

Anyone got an idea?

Thanks T

Messages sorted by: Reverse Date, Date, Thread, Author