bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [patch] new special variable: test argument


From: Stephane Chazelas
Subject: Re: [patch] new special variable: test argument
Date: Wed, 16 Dec 2015 18:36:52 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2015-12-16 16:03:14 +0100, Piotr Grzybowski:
> Dear All,
> 
>  one thing I missed for some time now, is the ability to access the
> argument passed to test, or any argument on the right hand side.
>  I needed it so I made a quick hack, which I attach as a reference.
>  It allows to access arg in the the -f $arg easily, e.g.:
> 
> [ -f /tmp/myfile ] && { echo "$^ is here"; head -1 "$^"; }
> 
>  I would be glad for some feedback. What I have in mind is: make $^ a
> special all-purpose variable, something in the lines of perls $_ .
>  The name '^' is just a first pick, and the implementation is a proof
> of concept more than complete patch, which I would like to make
> complete, if you find this of any interest .
[...]

What's wrong with

f=/tmp/myfile; [ -f "$f" ] && { echo "$f is here"; head -n 1 < "$f"; }

A few shells (es, zsh, others) have support for
lambdas/anonymous functions which could be another alternative.

In zsh:

(){ [ -f $1 ] && ... } /tmp/file

Or

() for f do [ -f $f ] && ...; done /tmp/file{1,2}
though here, there's hardly any benefit (beside the local scope)
over

for f in /tmp/file{1,2}; do [ -f "$f" ] && ...; done

$_ as the last argument of the last command or history expansion
are generic enough but having $^ as the last argument to test -f
seems too specific (and obscure) to me and a waste of a special
variable.

-- 
Stephane





reply via email to

[Prev in Thread] Current Thread [Next in Thread]