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

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


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

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

in both PowerShell and Bash.

Bash still supports the ancient way (backticks), butPowerShell cleaned up the syntax and removed redundantconstructs such as the two different command substitutionsyntaxes. This frees up the backtick for a differentuse in PowerShell: in POSIX ksh, the backslash is used asescape character, but that would be very painful inPowerShell because the backslash is the traditional pathcomponent separator in Windows. So, PowerShell uses the(now unused) backtick for escaping.


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

For example:

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


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