How can I make a Windows shortcut that launches Bash and runs commands?

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?


Solution 1:

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"

Solution 2:

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...] [CommandLine]

Arguments to run Linux binaries:

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

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

--
    Pass the remaining command line as is.

For further details, read WSL interoperabiltity with Windows.