53

Most systems provide an open command (alternately known as start, cygstart, xdg-open etc.) which opens a file in the desktop environment's "default" application, whatever that is.

For example, from Powershell when I enter:

PS> start form.pdf

The document pops open in Edge.

Can Bash-on-Ubuntu-on-Windows do this?

15 Answers 15

67

A recent answer for WSL 2: Just do wslview slides.pdf.

This is a part of the wslu collection and was pre-installed in my Ubuntu. Unfortunately, the documentation is a bit sparse.

All the cmd.exe variants didn't work for me, because it doesn't want to open inside a path like \\wsl$\…

8
  • 10
    Nice, and wslview . opens the current directory in Windows Explorer.
    – ben3000
    Nov 17, 2020 at 7:50
  • 1
    This is so good
    – trallnag
    Jan 24, 2021 at 23:17
  • Thanks, I've marked this as the accepted answer as WSL2 becomes the more common deployment. NB: On my system, this maps to a fake network share like \\wsl$\Ubuntu-20.04, which is on disk as something like C:\Users\Cheezmeister\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fnegsc\LocalState\rootfs Jun 8, 2021 at 22:04
  • 1
    wslview takes between 2–4 seconds to open an application (on my device). cmd.exe and explorer.exe—when they work—open applications almost immediately. (Can confirm that cmd.exe does not want to deal with UNC/network paths.) Oct 14, 2021 at 23:49
  • 1
    Works for WSL 1 on Windows 10. Nov 26, 2021 at 15:27
44

Since the Windows Linux interop started working you can now call:

cmd.exe /C start <file>
4
  • Works for me for relative paths, for absolute paths I wrote a script to substitute /mnt/c by c: ... | sed 's/\/mnt\/\(.\)/\1:/1' | xargs cmd.exe /C start
    – Michael
    Oct 4, 2017 at 20:58
  • 2
    Nice solution. We can also access files on the Linux filesystem using info from superuser.com/q/1110974/66714. Something like cmd.exe /c start "%localappdata%/lxss/$(readlink -f $some_relative_path)" Now we're cooking with fire! Oct 8, 2017 at 0:45
  • If your cmd doesn't want to open, see my answer.
    – primfaktor
    Nov 10, 2020 at 11:25
  • I guess Martijn's answer doesn't work in WSL 2 anymore.
    – primfaktor
    Nov 10, 2020 at 11:25
9

As Martijn noted this is the correct way to execute/open a Windows application/file.

cmd.exe /C start <file>

I found it very useful to work this into a bash script that I keep in a folder that is in my system path. I name it start and do chmod 0744 to the file to make it executable. The $* means it will pass all of the command line arguments you provided to the script to cmd.exe.

#!/bin/bash
cmd.exe /c start "Launching from BASH" "$*"

With this command in my system path I can commands like this in Linux that open in Windows:

  1. start FileXYZ.pdf // Opens the PDF in the default assigned PDF viewer in Windows
  2. start explorer . // Opens current WSL folder in the Windows Explorer
  3. start MyApp.exe // Launches the Windows application
4
  • 1
    The only problem is that this solution does not work with files which have whitespaces in their name. Nov 5, 2019 at 13:56
  • @h3nrik Great point. I guess I don't open a lot of files with spaces in Linux. I updated the script it to handle that. Nov 5, 2019 at 16:12
  • On Linux filenames with whitespaces are usually handled with backslashes like a\ whitespace.pdf. Starting your script like start.sh a\ a.pdf b\ b.pdf would not work. Nov 6, 2019 at 9:56
  • 1
    explorer.exe . opens the current path in Windows Explorer
    – johnny
    Mar 3, 2020 at 6:40
8

to expand on Martijn's answer, you can put

alias start='cmd.exe /C start'

in your .bashrc to get expected windows behavior, eg start . opens explorer in current dir.

1
  • 1
    To access bashrc, you use vi(vim) or nano by simply typing nano ~/.bashrc to edit the file.
    – Agent 0
    Feb 8, 2021 at 23:22
4

This worked much better for me:

explorer.exe `wslpath -aw <path>`
2
  • Normally I use the wslview solution above, but that was not working for opening a file in Notepad. This worked beautifully though! Dec 5, 2022 at 10:24
  • Update: also works on Excel files! Dec 18, 2022 at 14:45
2

This depends on whether you want to A) launch a linux program inside WSL or B) you want to launch a windows program from a bash shell prompt.

If B) then yes if you install cygwin/bash. For example install git for windows and you have a system running under windows with bash. Then you can just run start, actually it is included as a script:

$ cat /usr/bin/start
#!/usr/bin/env bash
# Copyright (C) 2014, Alexey Pavlov
#   mailto:alexpux@gmail.com
# This file is part of Minimal SYStem version 2.
#   https://sourceforge.net/p/msys2/wiki/MSYS2%20installation/
# File: start

cmd //c start "${@//&/^&}"

If A) then it gets much more difficult especially if you want to launch a linux program to display a .pdf in a GUI window. Note that Windows knows to associate a default application to open a pdf file but WSL doesn't have the information. So even if you did get a Desktop running under WSL you would need to associate a linux GUI app to open the pdf.

Note to clarify, within WSL you execute linux executables not windows executables:

(WSL):~# file /bin/gzip
/bin/gzip: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=68cc3c090405cf6d40e97d2ff58085fd26940602, stripped

(WSL):~# file /mnt/c/Program\ Files/Internet\ Explorer/iexplore.exe
 /mnt/c/Program Files/Internet Explorer/iexplore.exe: PE32+ executable (GUI) x86-64, for MS Windows

(WSL):~# /mnt/c/Program\ Files/Internet\ Explorer/iexplore.exe
 bash: /mnt/c/Program Files/Internet Explorer/iexplore.exe: cannot execute binary file: Exec format error
1

eopen can open various files (, directories and URI) within WSL.

https://github.com/ko1nksm/eopen-ecd

Examples

# Open directory with (latest used) Explorer
eopen ~/.config/

# Open directory with new instance of Explorer
eopen -n ~/.config/

# Opens with Windows default application
eopen image.png

# Opens with Windows text editor
eopen -e ~/.bashrc

# Use sudo to edit the unowned file
eopen -e --sudo /etc/hosts

# Opens with Windows default browser
eopen http://google.com

# Open files and directories under Windows
eopen C:/Windows

# Open files and directories under Network shared folder
eopen //server/shared

# Others
eopen mailto:user@example.com   # Mail protocol
eopen calculator:               # Application
eopen shell:Personal            # Shell commands
eopen :MyComputerFolder         # Shorthand for shell:
eopen shell:::{2559a1f8-21d7-11d4-bdaf-00c04f60b9f0} # CLSID
eopen :                         # Current Explorer location
eopen :/workspace               # Relative path from current Explorer location
1

You can call powershell's Start-Process command from within WSL:

powershell.exe -Command Start-Process file

For making this also work with absolute paths, you can use the wslpath -wa command to translate the path to a windows-path.

powershell.exe -Command Start-Process `wslpath -wa /absolute/path/to/file`

This has an advantage over the cmd.exe solution: for mounted network shares, wslpath produces an UNC path like \\server\share\. These UNC paths can't be handled by cmd.exe.

1

I've found that explorer.exe works pretty well at finding the correct resolved path (even of mounted network directories) and launching the default tool. The one gotcha is that you can't have paths in the filename, so you need to create a little helper function/script to launch explorer correctly, e.g.:

win() { 
    # get full unsymlinked filename 
    file=`readlink -e $1` 
    dir=$(dirname "$file") 
    base=$(basename "$file") 
    # open item using default windows application 
    (cd "$dir"; explorer.exe "$base")
}

Update: Ngo pointed out another script, wslpath, that does the path conversion, so you can call explorer.exe directly on the path (after converting). Then the function above becomes trivial and can easily be made an alias.

1
  • Excellent solution!
    – Stabledog
    Mar 12, 2020 at 15:46
1

Try this:

function open {
  cmd.exe /C start "$1" /C bash
}

If you want it work in every time just add it to the .bashrc:
enter link description here

1

The best and easiest way to open the files with the default application is using explorer.exe if you have WSL.

$explorer.exe <file>:

$explorer.exe form.pdf: that command line uses the windows explorer to open the file with the default program.

With explorer.exe you can also open a folder in your WSL with the windows file explorer, just insted of using a file as parameter you can specify a directory:

$explorer.exe <directory>:

$explorer.exe .: Opens the file explorer of windows in the current location.

1

Building on the idea proposed by @zx485 and @Nullcito here, you can use this:

function start() {
  cmd.exe /C start "$(wslpath -wa $1)" /C bash
}

You just need to pass the file and it will convert to the correct Windows path.

0

Try using wsl-open. It opens files with their standard-windows-application and can be downloaded here: https://github.com/4U6U57/wsl-open.

0

xdg-open in Ubuntu 20 could open http URL's but not the local files. Upgrade here: https://github.com/cpbotha/xdg-open-wsl

0

If you have fzf installed, you can add the following function into your .bashrc to use it for fuzzying finding and launching a Windows application for the path you choose:

function open() {
  INPUT=$(fzf --preview 'bat --color=always {}' --preview-window=right:50%)
  # Check for path before launching, in case fzf invocation was cancelled
  if [[ -e $INPUT ]] ; then
    cmd.exe /C start $(echo $INPUT)
  fi
}

The fzf command references bat as well, for syntax highlighting in the preview pane. If you do not have bat installed, simply replace 'bat --color=always {}' with 'cat {}' instead. The if conditional is used to check for an empty path so that cmd.exe will not pop-up if you run fzf and then cancel out of it without selecting a path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.