简体   繁体   English

如何将包含空格的文件名作为参数传递给命令行程序?

[英]How to pass a file name containing spaces as an argument to a command-line program?

Whenever I try to add a file with whitespace in its name to git, it shows an Error as每当我尝试将名称中带有空格的文件添加到 git 时,它都会显示错误为

"fatal: pathspec 'Tic' did not match any files"

Since I was New to git and Linux based terminal I have no idea how to do it.因为我是 git 和基于 Linux 的终端的新手,所以我不知道该怎么做。

Screen-shot or my error:屏幕截图或我的错误: 在此处输入图像描述

您应该能够引用文件名(例如“文件名”),或使用转义序列(例如文件\<空格>名称)。

To expand on @ergonaut's correct answer, just for the sake of clarity, this is actually neither a git nor a Linux issue.为了扩展@ergonaut 的正确答案,只是为了清楚起见,这实际上既不是 git 也不是 Linux 问题。 This is just a general requirement for command lines across the board.这只是全面命令行的一般要求。

On any command line, each word (or in this case, string of words) is evaluated separately as either a command or a parameter to a command.在任何命令行上,每个单词(或在本例中为单词串)都被单独评估为命令或命令的参数。 So, for example, git's add command is expecting a single parameter to come immediately after it (a filename).因此,例如,git 的add命令期望在它之后紧跟一个参数(文件名)。 In this case, the next word it sees is just "Tic".在这种情况下,它看到的下一个单词就是“Tic”。 Since it's only looking for a single parameter, it stops evaluating anything else at that point and that's why it's complaining about not being able to find the "Tic" file.因为它只寻找一个参数,所以它停止评估其他任何东西,这就是它抱怨找不到“Tic”文件的原因。 When the words are enclosed in quotes, the entire string is evaluated as a single parameter, therefore fixing the issue.当单词用引号引起来时,整个字符串被评估为单个参数,从而解决了问题。

Always wrap filenames that contain spaces with quotes when using them on the command line.在命令行上使用包含空格的文件名时,请始终用引号括起来。 Or even better, avoid using spaces in filenames.或者更好的是,避免在文件名中使用空格。 :-) :-)

Try:尝试:

git add Tic\ tac\ toe.c

\ is used to escape special characters, though this is more bash related rather than git specific. \用于转义特殊字符,尽管这与bash相关而不是git特定。

Alternatively, you could put the name of the file in quotes.或者,您可以将文件名放在引号中。

git add "Tic tac toe.c"

开始写第一个单词,然后按“Tab”按钮使用自动完成,你会很高兴))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM