bash git bash中文件路径的正斜杠与反斜杠
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40396613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forward slash vs backward slash for file path in git bash
提问by Saurav Sahu
I am running these two commands in Git bash.
我在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
creates file PatchesafterWGComment.txt
in D:/
创建文件PatchesafterWGComment.txt
中D:/
git diff > D:/Patches/afterWGComment.txt
correctly creates file afterWGComment.txt
in D:/Patches/
正确创建的文件afterWGComment.txt
中D:/Patches/
Note that D:/Patches/
folder is present before running the above commands.
请注意,D:/Patches/
在运行上述命令之前存在文件夹。
回答by Leon
Bash treats backslash as an escape character, meaning that the symbol following it is interpreted literally, and the backslash itself is dropped.
Bash 将反斜杠视为转义字符,这意味着它后面的符号按字面解释,反斜杠本身被删除。
$ echo $HOME
/home/user
$ echo $HOME
$HOME
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):
在 Windows 下,反斜杠用作路径分隔符,这会造成一些不便。幸运的是,在单引号内,反斜杠字符失去了它的特殊含义,并按字面处理(与任何其他字符一样,单引号除外):
$ echo '$HOME'
$HOME
Therefore, if you are going to copy&paste a Windows path into Git bash, put it inside single quotes:
因此,如果您要将 Windows 路径复制并粘贴到 Git bash 中,请将其放在单引号内:
git diff > 'D:\Patches\afterWGComment.txt'
回答by andlrc
Backslash is an escape character used to escape meta characters. This means you need to escape the escape:
反斜杠是用于转义元字符的转义字符。这意味着你需要逃离逃生:
D:\Patches\afterWGComment.txt
Alternative you can put your string in single quotes, which will make all characters literal:
或者,您可以将字符串放在单引号中,这将使所有字符成为文字:
'D\Patches\afterWGComment.txt'
Some meta characters: *
, ~
, $
, !
, ...
一些元字符:*
, ~
, $
, !
, ...
回答by Pragun
Well the Backslash (\)
in Linux generally means a escape character
. So in your case the backslash is escaping strings. Try with a cd "D:\Patches\afterWGComment.txt"
and you can see the difference.
那么Backslash (\)
在 Linux 中通常意味着escape character
. 因此,在您的情况下,反斜杠正在转义字符串。尝试使用 a cd "D:\Patches\afterWGComment.txt"
,您可以看到不同之处。
回答by Isaac
The back slash has a very long history in Unix (and therefore in Linux) of meanning: quote next character.
反斜杠在 Unix 中(因此在 Linux 中)的含义有很长的历史:引用下一个字符。
There are three ways to quote in the shell (where you type commands):
在 shell 中有三种引用方式(在其中键入命令):
- The backquote (
\
) - Single quotes (
'
) - Double quotes (
"
)
- 反引号 (
\
) - 单引号 (
'
) - 双引号 (
"
)
In the order from stronger to softer. For example, a $
is an special character in the shell, this will print the value of a variable:
按照从强到弱的顺序。例如,a$
是 shell 中的特殊字符,这将打印变量的值:
$ a=Hello
$ echo $a
Hello
But this will not:
但这不会:
$ echo $a
$a
$ echo '$a'
$a
$ echo "$a"
Hello
In most cases, a backslash will make the next character "not special", and usually will convert to the same character:
在大多数情况下,反斜杠会使下一个字符“不特殊”,并且通常会转换为相同的字符:
$ echo \a
a
Windows decided to use \
to mean as the same as the character /
means in Unix file paths.
To write a path in any Unix like shell with backslashes, you need to quote them:
Windows 决定使用\
与/
Unix 文件路径中的字符含义相同的含义。
要在任何类似 Unix 的 shell 中使用反斜杠编写路径,您需要引用它们:
$ echo \
\
$ echo '\'
\
$ echo "\"
\
For the example you present, just quote the path:
对于您提供的示例,只需引用路径:
$ echo "Hello" > D:\Patches\afterWGComment.txt
That will create the file afterWGComment.txt
that contains the word Hello
.
这将创建afterWGComment.txt
包含单词Hello
.
Or:
或者:
$ echo "Hello" > 'D:\Patches\afterWGComment.txt'
$ echo "Hello" > "D:\Patches\afterWGComment.txt"
$ echo "Hello" > "D:/Patches/afterWGComment.txt"
Quoting is not simple, it has adquired a long list of details since the 1660's.
引用并不简单,自 1660 年代以来,它需要一长串详细信息。