2

I need the result of a command to be taken as a parameter in another command.

command /opt <another command output>

On Bash Linux I would do

command /opt `another command`

What is the equivalent of the ` symbol that works in Windows Terminal?

In particular, I am using the command

'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}

where findstr is the windows equivalent to grep and the final command in the pipe is the equivalent to sed. The output of this command will be 1, 2 or 3, and I need this to be passed to the %HERE in the following line

'C:\Program Files\Display\display64.exe' /device %HERE /rotate 90

The following minimal example, does not work:

FOR /F %a in ('"C:\Program Files\Display\display64.exe"  /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}') do echo "%a"

What I am doing wrong? (I am new in Windows).

mklement0
  • 382,024
  • 64
  • 607
  • 775
Alejandro DC
  • 225
  • 3
  • 11

1 Answers1

4

Use (...), the grouping operator to pass output from a command as an argument to another command:

'C:\Program Files\Display\display64.exe' /device (
  'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace "- .*",""}
) /rotate 90

Note: $(...), the subexpression operator, works too, but (...) is usually sufficient and has no side effects; you only need $(...) for multiple (;-separated) statements - see this answer for details.


As for what you tried:

  • In POSIX-compatible shells such as Bash, `...` is the legacy form of a command substitution whose modern syntax is $(...) - while PowerShell supports $(...) too, (...) is usually preferable, as noted.

  • In PowerShell, ` serves as the escape character (only), analogous to \ in POSIX-compatible shells - see the conceptual about_Special_Characters help topic.

  • Your attempts to use FOR /F %a in ... and %HERE (which should be %HERE%) relate to the legacy cmd.exe shell on Windows, not to its successor, PowerShell, whose syntax differs fundamentally.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thank you. It works in PowerShell. For some reason it does not work in a shortcut icon I created in the desktop. I tried with `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "'C:\Program Files\Display\display64.exe' /device $('C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace '- .*',''}) /rotate 0"` but does not work. – Alejandro DC Feb 07 '22 at 17:27
  • 2
    `$( )` is good for inserting multiple statements seperated by a semicolon, like `$(echo hi; echo there)` – js2010 Feb 07 '22 at 17:35
  • I made it: `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "& 'C:\Program Files\Display\display64.exe' /device (& 'C:\Program Files\Display\display64.exe' /listdevices | findstr 22MP55 | %{$_ -replace \"- .*\",\"\"}) /rotate 0"`. I had to scape the double quote. – Alejandro DC Feb 07 '22 at 17:38
  • 1
    Indeed, @AlejandroDC: When calling via PowerShell's _CLI_ (`powershell.exe` for _Windows PowerShell_, `pwsh` for _PowerShell (Core) 7+_) from the outside using `-Command` / `c`, you need to _escape_ `"` chars. to be passed through as part of the command: `\"` works in principle, but can break on the `cmd.exe` side when calling from there. In that case, use `"^""` (sic) with `powershell.exe`, and `""` with `pwsh.exe`. See [this answer](https://stackoverflow.com/a/49060341/45375) for details. – mklement0 Feb 07 '22 at 18:51