Ubuntu: Command cd behavior [duplicate]



Question:

This question already has an answer here:

Since ubuntu 17.10, my scripts don't work the same way they used to do:

cd a*  

would change directory to the first directory beginning with a. Now, I get this error:

bash: cd: too many arguments  

I guess it now detects multiple arguments due to the multiple directories beginning with a and throws an error. How could I change the behavior of cd to the old way?


Solution:1

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  


Solution:2

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 <firas@fkraiem.org>  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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »