command substitution

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

  • Author
    Posts
  • #18598
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster
    $(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
    Profile photo of Garegin Asatryan
    Garegin Asatryan
    Participant

    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
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.