我的代码受到顶帖的启发,但经过修改后可以在 Windows 10 上的任何驱动器上运行,同时在本机 ubuntu(又名 WSL)上运行。
如果您只想要该功能,可以注释掉调试行。
cd
如果您只想输出路径,可以注释掉该行
function cdwin() {
# Converts Windows paths to WSL/Ubuntu paths, prefixing /mnt/driveletter and preserving case of the rest of the arguments,
# replacing backslashed with forwardslashes
# example:
# Input -> "J:\Share"
# Output -> "/mnt/j/Share"
echo "Input --> $1" #for debugging
line=$(sed -e 's#^\(.\):#/mnt/\L\1#' -e 's#\\#/#g' <<< "$1")
#Group the first character at the beginning of the string. e.g. "J:\Share", select "J" by using () but match only if it has colon as the second character
#replace J: with /mnt/j
#\L = lowercase , \1 = first group (of single letter)
# 2nd part of expression
#replaces every \ with /, saving the result into the var line.
#Note it uses another delimiter, #, to make it more readable.
echo "Output --> $line" #for debugging
cd "$line" #change to that directory
}