tags:

views:

134

answers:

1

I am wondering whether anyone came up with good use cases of the $^ and $$ automatic variables in Powershell. At the moment I am not seeing much use for them, at least from a programmer perspective.

When using PS interactively and executing a long command you could use .$^ to repeat it and with $$ you could get the last argument, but .$^ looks awkward enough to me that I don't consider that something people would use every day. Furthermore, since $$ only yields the last argument it's probably similarly difficult to use effectively.

But maybe I'm just mistaken and they have useful uses and I just can't see them.

+9  A: 

I use the $$ constantly during my day. I find little use for it in scripts but one operation I commonly do from the command line is to checkout a file and then open it in vim. The $$ reduces keystrokes in that circumstance.

PS> tf edit some\Long\Path\To\This\File\I\Need\To\Edit.cpp
Edit.cpp checked out
PS> gvim $$
JaredPar
Hmm, probably it shows that I am still more of a cmd user when it comes to the shell. Old habits die hard. And probably I would just have used up-arrow and replaced "tf edit" by "gvim". But then, I don't miss what I've never used and so I don't even know that there might be easier ways in some circumstances :)
Joey
@Johannes, I'm the same way. I find it difficult to break old bash and cmd.exe habit.s This is one change though that definitely paid dividends. The keystrokes are now muscle memory.
JaredPar