Skip to content

Instantly share code, notes, and snippets.

@benjamingwynn
Last active March 20, 2023 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benjamingwynn/a778aa6cf2dd461dc18978f245bd90c1 to your computer and use it in GitHub Desktop.
Save benjamingwynn/a778aa6cf2dd461dc18978f245bd90c1 to your computer and use it in GitHub Desktop.
macOS open command for WSL (Windows Subsystem for Linux)
#!/bin/bash
# open
# macOS open command for WSL (Windows Subsystem for Linux)
# written by benjamin gwynn (https://benjamingwynn.com)
# get latest version here: https://gist.github.com/benjamingwynn/a778aa6cf2dd461dc18978f245bd90c1
#
# You can run this on WSL wuth no prior setup or changes to host Windows.
#
# See: * https://docs.microsoft.com/en-us/windows/wsl/interop#run-windows-tools-from-linux
# * https://stackoverflow.com/a/501295
#
[ $# -eq 0 ] && { echo "usage: open [path on NTFS] | [--copy [path on WSL]]"; exit 1; }
if [ $1 = "--copy" ]; then
if [ -z "$2" ]; then
echo "usage: open [--copy [path on WSL]]"; exit 1;
else
mkdir -vp /mnt/c/WSLTemp
cp -v $2 /mnt/c/WSLTemp/
f="$(basename -- $2)" # get just the filename
cd /mnt/c # cd to C: drive to supress cmd warnings about not being on the right path
cmd.exe /c start "" c:\\WSLTemp\\$f
fi
else
if [ `stat --file-system --format=%T $1` = "v9fs" ]; then # v9fs check here isn't perfect, but works for now
cmd.exe /c start "" $1
else
echo "Windows applications cannot access the WSL filesystem, if you want to open this file with a Windows application anyway, re-run open with the --copy argument to copy the file to C:\WSLTemp on the host filesystem. Modifications to this file will not be saved."
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment