1

I created a path with spaces, and when I try to change directory I get "too many arguments" error message despite escaping the spaces or quoting the path :

Here are the tests I made :

# creating a path with spaces in it
$mkdir -p "01.Silly Path/0.2 With Plenty of /0.3 spaces"

# Trying to cd
$cd 01.Silly\ Path/0.2\ With\ Plenty\ of\ /0.3\ spaces/
bash: cd: too many arguments

# Trying with sh
$sh
$ cd 01.Silly\ Path/0.2\ With\ Plenty\ of\ /0.3\ spaces/
$ pwd
/xxx/Documents/dev/01.Silly Path/0.2 With Plenty of /0.3 spaces

# Trying to create a file using the problematic path : it works
$echo "dummy file test" > 01.Silly\ Path/0.2\ With\ Plenty\ of\ /0.3\ spaces/dummy_file.t
$cat 01.Silly\ Path/0.2\ With\ Plenty\ of\ /0.3\ spaces/dummy_file.t
dummy file test

# But command cd fails
$cd 01.Silly\ Path/0.2\ With\ Plenty\ of\ /0.3\ spaces/
bash: cd: too many arguments

$bash --version
GNU bash, version 5.2.15(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
lde@ldedebian ~/Documents/dev  $
$

# Tried quoting instead of escaping : still having failure with bash
$( cd "01.Silly Path/0.2 With Plenty of /0.3 spaces/" ; pwd)
bash: cd: too many arguments

# But works fine in zsh and sh
$sh -c ' cd "01.Silly Path/0.2 With Plenty of /0.3 spaces/" ; pwd '
/home/xxx/Documents/dev/01.Silly Path/0.2 With Plenty of /0.3 spaces
$zsh -c ' cd "01.Silly Path/0.2 With Plenty of /0.3 spaces/" ; pwd '
/home/xxx/Documents/dev/01.Silly Path/0.2 With Plenty of /0.3 spaces

# And the issue seems to be related to spaces indeed
$mkdir -p "01.Silly_Path/0.2_With_Plenty_of_/0.3_spaces"
$( cd 01.Silly_Path/0.2_With_Plenty_of_/0.3_spaces/; pwd)
/home/xxx/Documents/dev/01.Silly_Path/0.2_With_Plenty_of_/0.3_spaces

Did I miss something about Bash and spaces or is it a bug ?

2
  • 4
    I can not reproduce this issue with cd in Bash after creating the directory structure with the mkdir command that you show. Have you overridden the built-in cd command with a shell function that does not properly quote the path argument? What does type -a cd output in your Bash shell?
    – Kusalananda
    Commented Mar 8 at 9:59
  • 1
    Indeed there's a function cd installed by gvm. Removing that right away. Thanks Commented Mar 8 at 12:40

1 Answer 1

2

You confirmed in comments that you had some software installed that overloaded the shell's built-in cd utility with its own shell function. That shell function contains a bug, which makes it split its pathname argument on spaces (possibly due to forgetting to double quote a variable expansion), leading to the issue you observe.

Uninstalling that software, or at least removing the shell function with

unset -f cd

... would likely resolve the issue.

You could also opt for correcting the issue by changing \$* to \"\$@\" and $* to "$@" in this file.

2
  • IFS=; set -o noglob or switching to zsh where split+glob is not done upon unquoted parameter expansion might also help. Commented Mar 8 at 16:44
  • type -a cd was extremely helpful in comparison to my attempt to discover masking programs with which cd. Commented Mar 9 at 13:04

Not the answer you're looking for? Browse other questions tagged .