Skip to content

Instantly share code, notes, and snippets.

@dreamcat4
Created April 29, 2017 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreamcat4/b56fde008c738791c3435220b86b6d04 to your computer and use it in GitHub Desktop.
Save dreamcat4/b56fde008c738791c3435220b86b6d04 to your computer and use it in GitHub Desktop.
Change a Unix path in to Windows path for WSL
winpath() {
# get the Windows user path, convert to Unix line endings
user_path=$(echo "$(/mnt/c/Windows/System32/cmd.exe /C echo %HOMEDRIVE%%HOMEPATH%)" | tr -d "\r")
# expand the specified path
target_path=$(readlink -f $1)
# change ~ to $user_path
if grep -q "^/home/" <<< $target_path; then
temp_user_path=$(echo "$user_path" | sed -e 's|\\|/|g' -e 's|^\([A-Za-z]\)\:/\(.*\)|/mnt/\L\1\E/\2|' -e 's|^M$||')
# if there was something after ~, add it to the end of the $user_path
if grep -q "^/home/\(.*\)/\(.*\)" <<< $target_path; then
target_path=$temp_user_path$(echo "$target_path" | sed -e 's|^/home/*/\(.*\)|/\2|')
# if there was nothing after ~, $target_path is $user_path
else
target_path=$temp_user_path
fi
fi
# check if a Windows path is getting parsed
if grep -q "^/mnt/[a-z]" <<< $target_path; then
# swap /mnt/[a-z] with [A-Z]: and / with \
echo $(echo "$target_path" | sed -e 's|^\(/mnt/\([a-z]\)\)\(.*\)|\U\2:\E\3|' -e 's|/|\\|g')
else
# return the user's home directory if a Unix path was parsed
echo $user_path
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment