How to create a Windows batch script that opens both WSL Ubuntu and Firefox

Viewed 1011

I'm trying to write a pretty simple program -- test.bat. This, of course, will be executed within command.exe in Windows. I would like for this program to start Firefox in Ubuntu but also leave the terminal with Ubuntu running.

I've tried the following lines in the batch file:

ubuntu -c firefox
ubuntu /c firefox
ubuntu echo firefox
ubuntu firefox
ubuntu | firefox

... but none of these commands are launching Firefox. Why?

In other words, I'd like to:

  • Create batch file on the Windows Desktop
  • Be able to double-click the batch file to run
  • command.exe will run the batch file
  • The batch file will run Ubuntu in WSL
  • The batch file will open Firefox within Ubuntu

How do I pass the proper arguments to the ubuntu command to do this?

1 Answers

Assuming that you are able to run Firefox from within the instance (although you don't mention in the question that you can), it's likely that you need environment variables that are set within ~/.bashrc or ~/.bash_profile (perhaps $DISPLAY?).

If that's the case, then those likely aren't getting sourced when you attempt to script the launch using the commands you tried.

Try wsl -e bash -lic "firefox" to force the login and interactive startup scripts to be sourced. wsl.exe is the more up-to-date way to start WSL, and it's much more flexible than ubuntu.exe.

Update

From the comments, it sounds like you want to keep the shell open after launching. This should be:

wsl -e bash -lic "firefox & exec bash -li"

I don't have Firefox installed in any of my WSL instances at the moment, but I did test that command-line with xeyes, and it worked.

What it does:

  • Starts Ubuntu with the first bash command, with the login and interactive flags that we discussed above.
  • The first bash then executes firefox & to run it in the background, which allows the next command to run immediately. Without the &, you would need to exit Firefox, and only then would it execute the next command.
  • After putting Firefox in the background, the exec bash -li replaces the first bash (that launched Firefox) with a new bash, allowing the terminal to stay open in the shell.

If you have issues with that in a batch file, try:

start wsl -e bash -lic "firefox & exec bash -li"
Related