Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Powershell ${^} variable?

I recently learned that you can use special characters in PowerShell variables, e.g.: ${hello world!}, but then I stumbled across this:

${^}

What does it do? I first thought it referenced the newest created variable, but it does other stuff I haven't really figured out.

like image 552
Sigurd Garshol Avatar asked Mar 31 '26 15:03

Sigurd Garshol


2 Answers

The documentation says:

Contains the first token in the last line received by the session.

Examples:

dir c:\windows 🡒 dir

0..9 🡒 0

&{ dir } 🡒 &

It was likely introduced to get the last command used (dir, copy, rm, ...), but in reality that will only work for the most simplest cases, and thus it's not very useful.

The corresponding $$ returns the last token from the last line.

Note: The curly braces {} are only necessary for variable names containing characters which are not allowed in variables, except automatic variables (look here). In this case, you can omit them:

$^

like image 121
marsze Avatar answered Apr 02 '26 12:04

marsze


It shows the first word/token in the last executed command.

like image 39
programmer365 Avatar answered Apr 02 '26 13:04

programmer365



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!