command substitution

This topic contains 3 replies, has 2 voices, and was last updated by  Don Jones 3 years, 9 months ago.

  • Author
  • #18598

    Garegin Asatryan

    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

    Don Jones

    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

    Garegin Asatryan

    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

    Don Jones

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

You must be logged in to reply to this topic.