64

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 Answers15

91

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

primfaktor
  • 1,065
47

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

cmd.exe /C start <file>
8

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
pseudosavant
  • 423
  • 4
  • 6
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.

MyrionSC2
  • 287
  • 3
  • 5
6

This worked much better for me:

explorer.exe `wslpath -aw <path>`
JW0914
  • 9,096
Ngo
  • 61
3

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.

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
Gilbert
  • 31
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

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

Rakesh
  • 650
marv
  • 11
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.

geronimo
  • 546
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

zx485
  • 2,337
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.

NotTheDr01ds
  • 28,025
khaous
  • 11
  • 1
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

mehmet
  • 185
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.

treehead
  • 655