How can I make a Windows shortcut that launches Bash and runs commands?
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.
Related videos on Youtube
Keith
Updated on September 18, 2022Comments
-
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 about 5 yearsThank 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 about 5 yearsThank you! The
-c
argument worked great. It was tough to choose which one to accept as answer as they're both great. -
Keith about 5 yearsYes I'm aware. afaik I can only accept one answer.