3

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.

5
  • 1
    When you say that something didn't work, you have to say how it didn't work. (Remember, we can't see your screen.) Commented Jan 5, 2018 at 15:46
  • @Bill_Stewart I've added the output. I wrongly assumed that I was doing something very stupidly wrong which would be obvious just from the command. Commented Jan 5, 2018 at 16:21
  • @Bill_Stewart also, am I using the correct jargon? All my search seem to show me how to use a function within another function. :-/ Commented Jan 5, 2018 at 16:27
  • 1
    I can't test it, but git checkout -b $(SimplifyName("Test 1")) may be all you need... Commented Jan 5, 2018 at 16:27
  • @JamesC. that is it! Add it as an answer and I'll accept it. Unless someone points out that that command also silently breaks the internet! :D Commented Jan 5, 2018 at 16:28

1 Answer 1

5

$() 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")
Sign up to request clarification or add additional context in comments.

1 Comment

We don't use parentheses to call a function in PowerShell. It's valid syntax-wise but not correct semantically. It should really be git checkout -b $(SimplifyName "Test1").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.