无法使用cd前进 [英] Unable to move forward using cd
问题描述
在使用PowerShell前进时遇到问题。我可以上移目录,但不能下移。情况如下:
I'm having a problem moving forward through a path with PowerShell. I am able to move up the directory but not down. Here's the situation:
我打开PowerShell,然后输入 pwd命令,它显示我当前在 PS C:\Users\Robert Inspiron14>
I open PowerShell and type in the "pwd" command and it shows that I am currently in PS C:\Users\Robert Inspiron14>
我键入命令 cd。。现在我移到 PS C:\Users>
I type the command "cd.." and now I move to PS C:\Users>
然后我尝试通过键入以下内容来更改目录: cd C:\Users\Robert Inspiron14;我无法。不幸的是,由于缺乏声誉,我还无法发布图片。
I then attempt to change directories by typing: "cd C:\Users\Robert Inspiron14" and I am unable to. Unfortunately, I can't post a picture yet due to lack of reputation.
我可以在CMD中执行更改,但无法在PowerShell中执行更改。另外,我不知道如何将用户从 Robert Inspiron14更改为只是罗伯特。
I'm able to perform the change in CMD but not PowerShell. Also, I don't know how to change the User from "Robert Inspiron14" to just "Robert". Any help is appreciated!
推荐答案
以补充 Mathias R. Jessen的有用答案,其中提供了更多背景信息:
To complement Mathias R. Jessen's helpful answer with more background information:
引用包含空格的参数是常规在所有壳 中使用语法是必要的,因为 未加引号的空间用于分隔多个参数 。
Quoting an argument that contains spaces is a general syntactic necessity, in all shells, because unquoted spaces are used to separate multiple arguments.
不仅需要引用的空格,而且PowerShell的任何所谓的元字符 (不带引号的字符具有语法功能);例如,将路径传递到名叫 a; b
的目录也需要引用,因为;
否则被解释为语句分隔符。
It isn't only spaces that require quoting, but any of PowerShell's so-called metacharacters (characters that, when used unquoted, have syntactic function); for instance, passing the path to a directory literally named a;b
requires quoting as well, given that ;
would otherwise be interpreted as a statement separator.
有多个引用样式 :
There are multiple quoting styles:
-
由于您的路径是文字路径-它不包含变量引用或表达式-
'...'
-quoting(单 -quoting)是最佳选择。
使用...
-quoting( double -quoting),如果您的路径包含变量或子表达式,即,如果您需要可扩展字符串(插值字符串)。 [ 1]
有关PowerShell中字符串文字的概述,请参见此答案。
Since your path is a literal path - it contains no variable references or expressions -
'...'
-quoting (single-quoting) is the best choice.
Use"..."
-quoting (double-quoting) if your path contains variables or subexpressions, i.e. if you need an expandable string (interpolating string).[1]
See this answer for an overview of string literals in PowerShell.
另一种较不常见的解决方案是单独使用`
,PowerShell的转义字符: cd C:\Users\R注意Inspiron14
Another, less common solution is to individually escape the space characters with `
, PowerShell's escape character: cd C:\Users\Robert` Inspiron14
也请注意:
-
PowerShell的选项卡完成会根据需要自动应用引用。
PowerShell's tab completion automatically applies quoting as necessary.
cd ..
是PowerShell中内置的 function 的名称,目的是模拟 cmd.exe
的(可能是允许的)行为(如下所示);该函数执行语法正确的 Set-Location ..
调用(通过执行 $ {function:cd ..}
进行验证),并用空格将命令名与其参数分开。
cd..
is the name of a built-in function in PowerShell, whose sole purpose is to emulate cmd.exe
's (questionably permissive) behavior (see below); the function performs a syntactically correct Set-Location ..
call (verify by executing ${function:cd..}
), with a space separating the command name from its argument.
不幸的是, cmd.exe
'内置的 cd
命令决定不强制执行其通常的语法规则,以及启用的调用,例如 cd C:\程序文件
。
Unfortunately, cmd.exe
's built-in cd
command decided not to enforce its usual syntax rules, and enabled calls such as cd C:\Program Files
.
它不应该这样做:虽然方便乍一看,它构成了通常的规则中引起混乱的问题异常。
请注意, cmd.exe
' s 制表符完成正确地引用了包含空格的参数。
It should never have done that: While convenient at first glance, it constitutes a problematic exception from the usual rules that invites confusion.
Note that cmd.exe
's tab completion properly quotes arguments that contain spaces.
类似地, cd ..
是不幸的是,作为正确的 cd ..
的语法例外替代方式-请参见有关详细信息,请参见此答案。
Similarly, cd..
was unfortunately allowed as as syntactically exceptional alternative to the correct cd ..
- see the comments on this answer for details.
[1]注意如果在路径中使用 variable 引用,则严格不需要 ...
引用,只要任何 literal 组件都不需要引用;例如, $ HOME\foo
可以不加引号,而 在
附近; $ HOME\foo bar 是必需的。使用 subexpressions (
$(...)
),规则变得更加复杂,因此最简单的方法是始终始终使用 ......
[1] Note "..."
-quoting isn't strictly necessary if you use variable references in a path, as long as any literal components do not require quoting; e.g., $HOME\foo
is fine without quoting, whereas the "
around "$HOME\foo bar"
are required. With subexpressions ($(...)
), the rules get more complicated, so the simplest approach is to always use "..."
-quoting with them.
这篇关于无法使用cd前进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!