Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Function Output As Argument Value To Git Command

Tags:

git

powershell

How to pass the output of a powershell function as an argument to a command?

I was unsuccessful with what I thought was a very straightforward command:

git checkout -b SimplifyName("Test 1")

output is an error:

fatal: 'Test 1' is not a commit and a branch 'SimplifyName' cannot be created from it

where, for the sake of this question,

function SimplifyName ([string] $str = "UNDEFINED") {
    $result = $str.Trim().ToLowerInvariant() -replace "[^a-z0-9]+","_"
    Write-Output $result
}

From what I understand, anything that follows -b is taken as space-delimited string arguments for the git checkout -b command.

I am struggling to find a good help resource for this too since I am possibly using incorrect terminology.

like image 553
bPratik Avatar asked Dec 02 '25 20:12

bPratik


1 Answers

$() is a subexpression operator, it means 'evaluate this first, and do it separately as an independent statement'.

Here it's used to evaluate the function and then use the output from it for the git command:

git checkout -b $(SimplifyName "Test 1")
like image 185
henrycarteruk Avatar answered Dec 05 '25 15:12

henrycarteruk



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!