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

8,457

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.

Share:
8,457

Related videos on Youtube

Keith
Author by

Keith

Updated on September 18, 2022

Comments

  • Keith
    Keith over 1 year

    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?

  • Keith
    Keith about 5 years
    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
    Keith about 5 years
    Thank you! The -c argument worked great. It was tough to choose which one to accept as answer as they're both great.
  • Keith
    Keith about 5 years
    Yes I'm aware. afaik I can only accept one answer.