[Haifux] [HAIFUX LECTURE] Command-line utilities: Tips and tricks (part II) -- Eli Billauer

[Haifux] [HAIFUX LECTURE] Command-line utilities: Tips and tricks (part II) -- Eli Billauer

Daniel Shahaf d.s at daniel.shahaf.name
Mon Aug 19 16:01:48 IDT 2013


Michael Shiloh wrote on Sun, Aug 18, 2013 at 16:36:35 -0700:
>
>
> On 08/18/2013 02:08 PM, Daniel Shahaf wrote:
>> Some of the odd corners of shell syntax are quite useful in interactive
>> usage.  For example:
>>
>> % (){ foo $1 bar } 24
>> to run a command several times (recalling it from history) and change
>> some parameter around the middle of the command line without having to
>> scroll to it every time.
>
>
> wait, can you explain  this?

Sure.  It's a zsh-specific syntax for an anonymous function with
arguments.  In effect it's an anonymous block.  For example:

% (){ printf "$1\n" 04 } "%s"  
04
% (){ printf "$1\n" 04 } "%d"  
4
% (){ printf "$1\n" 04 } "%e"  
4.000000e+00

In interactive usage I sometimes find myself wanting to run a command
several times in a row with one argument changed.  When that argument is
in the middle of a (potentially multi-line) command, I find it easier to
change it between runs by using an ad-hoc anonymous function to move the
argument-to-be-changed to the end of the input.  Another case where that
would be useful is when the argument appears in two places in the
command --- using an anonymous function allows changing the argument in
just one place rather than two.



More information about the Linux-il mailing list