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: konsolebox
Subject: Re: [patch] new special variable: test argument
Date: Thu, 17 Dec 2015 03:44:53 +0800

On Wed, Dec 16, 2015 at 11:03 PM, Piotr Grzybowski <narsil.pl@gmail.com> wrote:
>  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 .

Just my quick thoughts:

Pros:
1) It could make code smaller.
2) It could make writing code less prone to typos.
3) It could make writing code faster.

Cons:
1) The constant assignments could slow down bash significantly but if
the string argument can simply be reused then maybe not.
2) It could make code look hacky or less readable like Perl's,
although it might depend on every user.

P.S. Because of this, I actually got an idea of using a variable for
storing values of optional arguments passed to `return`:

function a {
    return 0 a b c d  # If no arguments are made, assign a static
empty array to `$.` to prevent slowdown when feature is not used. The
assigned array should be frozen, or perhaps readonly.
}
a
echo "${.[@]}"  # a b c d

It would certainly be useful.  I might create a patch and propose it
later if it's workable and efficient enough.



reply via email to

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