Unable to move forward using cd

前端 未结 2 1665
生来不讨喜
生来不讨喜 2021-01-23 12:09

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:

I open PowerShell and type

相关标签:
2条回答
  • 2021-01-23 12:56

    Before PowerShell can execute your cd command, it needs to parse it, and PowerShell's parser interprets your command like this:

    cd  C:\Users\Robert Inspiron14
    \/  \_____________/ \________/
    Command Name  |            |
                 argument 1    |
                             argument 2
    

    In other words, C:\Users\Robert and Inspiron14 are interpreted as separate arguments.

    Neither argument is a path to a valid directory, so cd (or rather Set-Location for which cd is an alias) throws an error.

    You can force PowerShell to recognize C:\Users\Robert Inspiron14 as a single string argument by qualifying its boundaries using quotation marks (both " and ' will work):

    cd 'C:\Users\Robert Inspiron14'
    

    You can read more about how PowerShell parses command expressions in the about_Parsing help topic

    0 讨论(0)
  • 2021-01-23 13:05

    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.

    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:

    • 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.

    • Another, less common solution is to individually escape the space characters with `, PowerShell's escape character: cd C:\Users\Robert` Inspiron14

    Also note:

    • PowerShell's tab completion automatically applies quoting as necessary.

    • 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.


    Contrast with cmd.exe:

    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.

    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.

    Similarly, cd.. was unfortunately allowed as as syntactically exceptional alternative to the correct cd .. - see the comments on this answer for details.


    [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.

    0 讨论(0)
提交回复
热议问题