What's the cmd/PowerShell equivalent of back tick on Bash?

24,297

Solution 1

The PowerShell syntax is based on the POSIX ksh syntax (and interestingly not on any of Microsoft's languages like CMD.EXE, VBScript or Visual Basic for Applications), so many things work pretty much the same as in Bash. In your case, command substitution is done with

echo "Foo $(./print_5_As.rb)"

in both PowerShell and Bash.

Bash still supports the ancient way (backticks), but PowerShell cleaned up the syntax and removed redundant constructs such as the two different command substitution syntaxes. This frees up the backtick for a different use in PowerShell: in POSIX ksh, the backslash is used as escape character, but that would be very painful in PowerShell because the backslash is the traditional path component separator in Windows. So, PowerShell uses the (now unused) backtick for escaping.

Solution 2

In PowerShell, you use $( ) to evaluate subexpressions...

For example:

PS C:\> "Foo $(./print_5_As.rb)"
Foo AAAAA

Solution 3

In CMD.EXE there is no direct equivalent. But you can use the FOR command to achieve what you want.

Do something like the following:

FOR /F "usebackq" %x IN (`./print_5_As.rb`) DO @echo Foo %x

or

FOR /F %x IN ('"./print_5_As.rb"') DO @echo Foo %x

You might need to set delimiter to something else than the default, depending on how the output looks and how you want to use it. More details available in the FOR documentation at https://technet.microsoft.com/en-us/library/bb490909.aspx

Share:
24,297

Related videos on Youtube

Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and & for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on July 05, 2022

Comments

  • Peter Mortensen
    Peter Mortensen almost 2 years

    Redirecting command output:

    For example:

    echo "Foo `./print_5_As.rb`"
    

    would echo "Foo AAAAA"

    • Jay Bazuzi
      Jay Bazuzi over 15 years
      You are asking two questions: one for CMD, and one for PowerShell.
    • jdigital
      jdigital over 10 years
      For cmd.exe, there's a nice alternative described here: stackoverflow.com/questions/2768608/…
  • PEZ
    PEZ over 15 years
    It's great reading answers like this where you get to learn how and why things are. Thanks! (I'd do +10 if I could.)
  • jordanpg
    jordanpg about 9 years
    Cumbersome, unlike it's uber-elegant bash equivalent: for x in `./print_5_As.rb`; do echo %x; done
  • matli
    matli over 8 years
    @jordanpg, you seem to miss the point that FOR in this case is not used for looping. The bash equivalent would still be as in the original post, without any for loop.
  • Gnubie
    Gnubie almost 7 years
    Only the first "word" of the output of print_5_As.rb is echoed if the output contains spaces.