command substitution

Welcome Forums General PowerShell Q&A command substitution

This topic contains 3 replies, has 2 voices, and was last updated by

 
Keymaster
4 years, 6 months ago.

  • Author
    Posts
  • #18598

    Participant
    Points: 1
    Rank: Member

    We all know that many unix shells have it and the format for powershell is $ (command ). But I can't find it documented anywhere. In the first edition of the book powershell TFM, it is mentioned, but the later edition lacks it. is it still around? I tried mkdir $ (1+3) and received error

    mkdir : A positional parameter cannot be found that accepts argument '4'.
    At line:1 char:1
    + mkdir $ (1+3)
    + ~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [mkdir], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,mkdir

    what gives?

  • #18599

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero
    $(dir)

    Is called a subexpression, and it's valid inside double quotes. Notice there is no space between the dollar sign and the parentheses. You used a space; that passed $ as the first argument, and 4 (the result of the expression 1+3) as a second parameter. The command didn't know what to do with the second parameter.

    If your goal was to create a directory named 4, you don't need to use a subexpression.

    mkdir (1+3)

    Would have worked. Parenthetical expressions are always evaluated first, and the result passed to whatever parameter.

  • #18600

    Participant
    Points: 1
    Rank: Member

    thanks a lot. BTW. what happened to the PowerShell TFM book, the publisher announced new editions, but there are nowhere to be found. what gives?

  • #18601

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    You'd have to ask them. I'm not involved with the book any longer.

The topic ‘command substitution’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort