git bash中文件路径的正斜杠与反斜杠 [英] Forward slash vs backward slash for file path in git bash

查看:812
本文介绍了git bash中文件路径的正斜杠与反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Git bash 中运行这两个命令.

I am running these two commands in Git bash.

为什么他们的行为有所不同?他们不是应该做同样的事情,还是我想念什么?

Why they behave differently? Aren't they supposed to do the same thing or am I missing something?

git diff > D:\Patches\afterWGComment.txt  

D:/

git diff > D:/Patches/afterWGComment.txt  

D:/Patches/

请注意,在运行上述命令之前,已存在D:/Patches/文件夹.

Note that D:/Patches/ folder is present before running the above commands.

推荐答案

Bash将反斜杠视为转义字符,这意味着其后的符号将按字面意义进行解释,并且反斜杠本身也会被删除.

Bash treats backslash as an escape character, meaning that the symbol following it is interpreted literally, and the backslash itself is dropped.

$ echo $HOME
/home/user
$ echo \$HOME
$HOME

在Windows中,反斜杠用作路径分隔符,这会带来一些不便.幸运的是,单引号内的反斜杠字符会失去其特殊含义,并且会按字面意义对待(与其他任何字符一样,单引号除外):

Under Windows, where backslash serves as a path separator, this causes some inconvenience. Fortunately, inside single quotes a backslash character loses its special meaning and is treated literally (as any other character, except a single quote):

$ echo '\$HOME'
\$HOME

因此,如果要将Windows路径复制并粘贴到Git bash中,请将其放在单引号内:

Therefore, if you are going to copy&paste a Windows path into Git bash, put it inside single quotes:

git diff > 'D:\Patches\afterWGComment.txt'

这篇关于git bash中文件路径的正斜杠与反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆