PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 21, 2022

[FIXED] What is the equivalent of the Bash ` (backquote/backtick) in Windows PowerShell?

 December 21, 2022     powershell, syntax     No comments   

Issue

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).


Solution

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.



Answered By - mklement0
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing