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