Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How should I write a path when a folder name contains spaces?

Quick answer

Quote the complete path value when passing it to a command. For example, in Bash use cat "/home/alex/Project Files/notes.txt"; in PowerShell use Get-Item -LiteralPath 'C:\Work\Project Files\notes.txt'. You can also escape spaces: Bash uses a backslash, while PowerShell uses a backtick for parameter values. These are different shell conventions, so use the form for the shell you are typing in [S1][S2].

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What’s the basic rule if a folder name has a space? Should I quote just the folder name or the entire path?

Geez

Quote the complete path value, from its beginning to its end, including the folder name and any filename. That makes the path one quoted value rather than separating it at the space.

For example, in Bash: cat "/home/alex/Project Files/notes.txt"

In PowerShell: Get-Item -LiteralPath 'C:\Work\Project Files\notes.txt'

These are example paths, not claims that those locations exist. Bash quoting prevents spaces and other characters inside the quotes from receiving their usual shell-special meanings [S1]. PowerShell’s parameter guidance says a string parameter value containing spaces must be quoted or have its spaces preceded by the backtick escape character [S2].

Follow-up question

Can you compare the quoted and escaped forms in both shells?

Geez

Here are two ways to write the same imaginary file path in each shell:

  • Bash, quoted: cat "/home/alex/Project Files/notes.txt"
  • Bash, escaping the space: cat /home/alex/Project\ Files/notes.txt
  • PowerShell, quoted: Get-Item -LiteralPath 'C:\Work\Project Files\notes.txt'
  • PowerShell, escaping the space in a parameter value:
Get-Item -LiteralPath C:\Work\Project` Files\notes.txt

Bash uses a backslash to escape a space [S1]. PowerShell uses a backtick for this parameter-value form [S2]. The escape marks are shell syntax, not a general path format to copy between shells. The PowerShell guidance concerns parameter values and does not establish how every native program parses its arguments [S2].

Follow-up question

How can I tell whether a backslash or backtick is part of the folder name or an escape mark?

Geez

Check which shell you are using before interpreting the mark. In Bash, the backslash in Project\ Files escapes the following space [S1]. In PowerShell, the backtick in Project Files` escapes the following space in a parameter value [S2]. In both cases, the mark is part of the command’s shell syntax, not a direction to add that mark to the folder name.

If you are unsure, use a quoted path instead: the opening and closing quotes make the value’s boundaries easier to see. Don’t paste an escaped form from one shell into the other; the documented conventions differ [S1][S2].

Follow-up question

What’s a quick template I can adapt, and what should I check if it still fails?

Geez

Use the pattern for the shell you are typing in, replacing the example path with the intended one:

  • Bash: COMMAND "/path/to/Project Files/notes.txt"
  • PowerShell: COMMAND -LiteralPath 'C:\path\to\Project Files\notes.txt'

COMMAND and the example locations are placeholders. If you choose escaping instead, Bash uses a backslash before each space [S1]; PowerShell uses a backtick before a space in a parameter value [S2].

If the command still fails, check that the folder and filename are spelled correctly, then consult that command’s help for how it accepts path arguments. Quoting addresses how the shell treats spaces; it does not establish that the location exists or that every command handles paths in the same way.

A practical checklist

  • Identify whether you are typing in Bash or PowerShell.

  • Quote the complete path value, including the spaced folder and filename.

  • If escaping spaces, use Bash backslashes or PowerShell backticks in the matching shell.

  • Check the path spelling if the command does not find the location.

  • Consult the command’s help if it still does not accept the path.

A few more useful answers

Can I use a backslash before a space in PowerShell?

The cited PowerShell parameter guidance specifies the backtick as the escape character for spaces, not Bash’s backslash convention [S2]. Use quotes or the PowerShell-specific form.

Do the quotes become part of the folder name?

In these examples, the quotes mark the path value supplied to the shell; they are not presented as characters in the folder name. Whether a particular path exists is a separate matter.

Does quoting guarantee that every command will accept the path?

No. Quoting addresses how the shell treats spaces. The cited PowerShell guidance does not settle how every native program parses arguments [S2], so check the command’s help if needed.

Sources

References used for this guide.

  1. [S1] Bash Reference Manual: Quoting
    gnu.org · Checked
  2. [S2] PowerShell about_Parameters
    learn.microsoft.com · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work