13

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?

30

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

cmd.exe /C start <file>
| improve this answer | |
  • 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! – Cheezmeister Oct 8 '17 at 0:45
3

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
| improve this answer | |
  • 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. – pseudosavant 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 at 6:40
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
| improve this answer | |
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
| improve this answer | |
1

This worked much better for me:

explorer.exe `wslpath -aw <path>`
| improve this answer | |
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.

| improve this answer | |
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.

| improve this answer | |
  • Excellent solution! – Stabledog Mar 12 at 15:46
1

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.

| improve this answer | |
0

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

| improve this answer | |

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.