Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Why Your Shell Command Breaks on Filenames With Spaces

'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…

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.

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

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

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

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

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

USD and Solaris: a practical CGI guide

In a team, many people touch one scene at once. The modeler fixes a mesh, the lookdev artist changes a material, the layout artist moves a prop, the lighter adjusts a lamp.

Test shows that mintupgrade does not upgrade Linux Mint 22.3 to 27

A test by ComputingForGeeks showed that mintupgrade interrupts the attempt to take Linux Mint 22.3 to 27 before changing any packages.

  • Mintupgrade cannot update Linux Mint 22.3 to Mint 27.
  • Tested in November 2026 on an updated Cinnamon desktop.
  • Mintupgrade directs to Ubuntu 24.04 for Mint 22 repositories.

Building WorkManager Jobs That Are Truly Idempotent — Not Just 'Usually Fine'

On Android, background execution is unpredictable. Google's official background processing library, WorkManager , is designed to be resilient: it guarantees that scheduled background tasks will…

  • WorkManager ensures at-least-once execution, not exactly-once, risking duplicate tasks.
  • FinLedger's idempotent workers process recurring tasks without duplication or corruption.

More from Sunday, 29 November 2026 →