{
  "id": 22514162,
  "title": "Why Your Shell Command Breaks on Filenames With Spaces",
  "url": "https://urgent.news/2026/11/29/why-your-shell-command-breaks-on-filenames-with-spaces",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-29T04:28:30.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/systemcraftdev/why-your-shell-command-breaks-on-filenames-with-spaces-1j55"
  },
  "original_language": null,
  "account": "Filenames containing spaces can cause issues when executing shell commands. The shell splits the input line into words using whitespace as the separator, which leads to problems when a filename includes spaces. For example, trying to change directory to a folder named \"My Documents\" using the command `cd My Documents` results in an error: \"cd: too many arguments.\" Similarly, running `ls My Documents` causes the shell to report two errors, as it interprets \"My\" and \"Documents\" as separate arguments instead of a single valid filename.\n\nTo resolve this, you can quote the filename, enclosing it in either single or double quotes. This tells the shell to treat the contents of the quotes as a single argument. For instance, using `cd \"My Documents\"` or `ls 'My Documents'` will correctly execute the command. Another method is to escape spaces with a backslash, like `ls My\\ Documents`. Additionally, you can let tab completion handle the quoting automatically by typing `cd My` and pressing the Tab key.\n\nWhen writing scripts, always quote variables containing paths to ensure they're passed as a single argument to commands. This prevents issues when paths contain spaces. For example, `ls \"$d\"` will work correctly, whereas `ls $d` will split the path into separate words.\n\nBe cautious when deleting files or running loops over filenames, as the same quoting rules apply to variables. Before deleting a file with a space in its name, quote the filename or run `ls` with the same arguments to verify the correct path is being used. Additionally, loop over filenames using the expanded pattern directly, like `for f in *.txt; do echo \"[$f]\"; done`, to avoid splitting filenames into separate parts.\n\nIn summary, always quote filenames and variables containing spaces to ensure shell commands interpret them correctly. This simple practice prevents common errors and ensures your commands behave as expected.",
  "summary": "'No such file' for a folder you can see, or 'too many arguments'? The shell splits on spaces before your command runs. Here's how quoting fixes it. Adapted from the Command Line Essentials Companion Guide . There's a folder called My Documents . You can see it in the file manager, and you can see it in ls . You try to open it from the terminal: $ cd My Documents bash: c d: too many arguments Or…",
  "summary_source": "publisher",
  "key_points": [
    "Filenames with spaces cause shell command errors",
    "Quoting filenames resolves the issue",
    "Escaping spaces or using tab completion as alternatives"
  ],
  "editors_take": "Properly quoting filenames and variables prevents shell commands from splitting single paths into separate arguments, which protects users from execution errors and accidental file deletions.",
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}