http://scz.617.cn:8/windows/202206120354.txt
Q:
When we use WSL shell on windows, it is annoying to convert the path back and forth, is it possible to make our life easier?
A: bluerust 2022-06-12
WSL provides a tool named wslpath, which can help convert the paths between Windows and Linux, the usage is very simple:
wslpath '<path>'
The quotes are necessary to avoid escaping the path when it is Windows format.
By virtue of this tool, we can add to following code to .bashrc:
function _pushd ()
{
[[ "$1" = *"\"* ]] && builtin pushd $(wslpath -a "$1") || builtin pushd "$1";
}
function _cd ()
{
[[ "$1" = *"\"* ]] && builtin cd $(wslpath -a "$1") || builtin cd "$1";
}
#
# pushd 'x:temp'
# cd 'x:temp'
#
# the path must be quoted with either double quotes or single quote
#
alias pushd='_pushd'
alias cd='_cd'
After updating the rc file, we need to source it:
source ~/.bashrc
Now, you can use the Windows path with cd and pushd commands:
pushd 'x:temp'
cd 'x:temp'
Please keep in mind that, if you omit the slash following the colon sign,wslpath would treat it as a directory name, for instance:
$ wslpath -a x:
/mnt/c/x:
原文始发于微信公众号(青衣十三楼飞花堂):在WSL中切换Windows/Linux路径表达方式
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论