Command cd behavior [duplicate]

This looks like a bug in Bash: per man builtins the old behaviour is the correct one.

   cd [-L|[-P [-e]] [-@]] [dir]
              Change the current directory to dir.  if dir is not supplied,
              the  value  of  the  HOME shell variable is the default.  Any
              additional arguments following dir are ignored.

You can report the bug on the bug-bash mailing list; more details here.


Actually, the bug was reported (long ago). If you want a fixed Bash now, now, now, here's how to do it properly (tested on 17.10, should work on others as well).

First create a directory to work in, for example:

mkdir ~/bash
cd ~/bash

Get the source package and the build dependencies:

apt-get source bash
sudo apt-get build-dep bash
cd bash-4.4

Edit config-top.h to change this (should be line 32)

#define CD_COMPLAINS

to this

/* #define CD_COMPLAINS */

Edit debian/changelog and add an entry like this at the top (you can also use the command dch -i):

bash (4.4-5ubuntu1+cd) artful; urgency=medium

  * Fix cd.

 -- Firas Kraiem <[email protected]>  Thu, 04 Jan 2018 21:11:22 +0900

The most important points are to append +foo to the current version number (foo can be any string of lowercase letters; and be careful if you use dch -i, it will increment the last number, so you need to revert it to the current one) and to use the correct release name (artful here). Finally, run dpkg-source --commit.

You can then run the debuild command, and if everything goes well (errors about debsign can be ignored) you should have some .debs in the parent directory, which you can install as usual (there is no need to install all of them, just the ones you have now; use dpkg -l | grep bash to find out).

Note that the version number of the new package is set so that you will automatically get any future update to bash; if the update does not fix the problem, you have to repeat the above process.


I found the same question: Ubuntu 17.04 — bash: cd: too many arguments. Apparently it is a bug.

The workaround proposed there is to create a function, based on the builtin cd command that overrides its behaviour:

cd(){ builtin cd "${@:1:1}"; }

Place the above line into the beginning of your script and then use cd a* as in Ubuntu 16.04. Use export -f cd if you want to use it in a sub shell.

You could define and export the function also in your ~/.bashrc file:

cd(){ builtin cd "${@:1:1}"; }
export -f cd