32

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?

12 Answers 12

27

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$\…

4
  • 3
    Nice, and wslview . opens the current directory in Windows Explorer.
    – ben3000
    Nov 17 '20 at 7:50
  • 1
    This is so good
    – trallnag
    Jan 24 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 at 22:04
  • A bit sad that this command does not work under Windows cmd so you can't have a cross-platform (WSL + host OS) solution. Jul 7 at 12:18
38

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 '17 at 20:58
  • 1
    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 '17 at 0:45
  • If your cmd doesn't want to open, see my answer.
    – primfaktor
    Nov 10 '20 at 11:25
  • I guess Martijn's answer doesn't work in WSL 2 anymore.
    – primfaktor
    Nov 10 '20 at 11:25
6

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.
    – 0x7d7b
    Nov 5 '19 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 '19 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.
    – 0x7d7b
    Nov 6 '19 at 9:56
  • explorer.exe . opens the current path in Windows Explorer
    – johnny
    Mar 3 '20 at 6:40
5

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.
    – Lulu
    Feb 8 at 23:22
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

This worked much better for me:

explorer.exe `wslpath -aw <path>`
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 '20 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

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

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.