14

I've created a windows shortcut with a target that looks like

"C:\Windows\System32\bash.exe" php /c/script.php

(I know I could just use my windows verison of php cli to run this, but out of curiosity)

When I click my shortcut it just pops open a cmd window and closes (where the script should take minutes to run.

Is there a way to pipe commands to bash.exe on windows?

2
  • It's simple, just wait and hello world, so i should see something. But the cmd window pops open for a moment and closes.
    – Keith
    Commented Mar 13, 2019 at 15:09
  • 1
    Many things I see that are odd about this question. If it were WSL, then "/c/script.php" would be "/mnt/c/script.php".. it it were CYGWIN or MINGW, then bash.exe wouldn't be in Windows/System32. What 'Unix' are you using? Commented Mar 13, 2019 at 15:44

2 Answers 2

12

If this is WSL, use bash.exe -c "command to run" as in

C:\Windows\System32\bash.exe -c "vi ~/.bashrc"

or

C:\Windows\System32\bash.exe -c "php /mnt/c/script.php"

2
  • Thank you! The -c argument worked great. It was tough to choose which one to accept as answer as they're both great.
    – Keith
    Commented Mar 13, 2019 at 17:19
  • Yes I'm aware. afaik I can only accept one answer.
    – Keith
    Commented Mar 13, 2019 at 17:48
11

First of all, bash.exe has been deprecated. You should use wsl.exe in command lines. Use Windows Insiders Builds 17063 and above for the interoperability feature in WSL. For your case, both of this may work:

wsl.exe php /mnt/c/MyFiles/test.php
wsl.exe --exec php /mnt/c/MyFiles/test.php

Here is the wsl.exe usage information:

Usage: wsl.exe [Argument] [Options...] [Command]

Arguments to run Linux binaries:

If no command line is provided, wsl.exe launches the default shell.

--exec, -e <Command>
    Execute the specified command without using the default Linux shell.

--
    Pass the remaining command line as is.

For further details, read WSL interoperability with Windows.

1
  • Thank you for the heads up about bash.exe's deprecation. I'll use wsl from here on out. And your example worked perfectly, thank you!
    – Keith
    Commented Mar 13, 2019 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .