windows - Bash中手动输入Windows路径,如何转换为POSIX路径?

标签 windows bash sh git-bash

当我在 Bash 中手动输入 Windows 路径时, 我想将其转换为 POSIX 路径并将其保存在变量中。 但不会转义转义字符。

我想输入 C:\Users\xiang\Downloads\Compressed 并将其转换为 /c/users/xiang/downloads/compressed


#!/bin/sh

read -rp "Enter a path: " pth

dir=$(echo "/$pth" | sed -e 's/\\/\//g' -e 's/://')

cd ${dir}

git status

read -n 1 -s -r -p "Press any key to continue"

exit 0;

enter image description here

最佳答案

问题是 Windows 路由中的反斜杠被认为是转义字符的一种方式。

这里你有两个选择,要么你用双反斜杠写路由 (C:\\Users\\...) 或者你可以使用 read -rp "Enter a path: "pth (注意 r)。

我个人更喜欢第二个选项,因为你不必告诉其他用户用双反斜杠写路由,而且更简单。

关于windows - Bash中手动输入Windows路径,如何转换为POSIX路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56891171/

相关文章:

java - 如何在 Windows 10 环境中从 Java 运行在 Ubuntu 上用 Bash 编写的 shell 脚本?

linux - M-^ 是什么意思? in stty 输出是什么意思?

bash - sh 和 bash 中 pgrep 的区别

python - 在Supervisord中通过sh脚本启动程序

linux - 如何从 pid 中找到 bash 脚本名称?

windows - 如何在 ghci 中使用数学符号(函数名)?

c - Windows 中的头文件 alloca.h

c - 编辑 etc\hosts 文件

bash - 使用 jq 分配多个输出变量

linux - Bash - 比较两个命令的输出