0

I edited my .bashrc to add more directories to $CDPATH. It looks like this:

export CDPATH=".:~:~/LEI/2_ano/2_smt:~/LEI/2_ano/1_smt"

But now every time I use the cd command, it prints the absolute path. For example, if I do cd ~/Downloads, the result is

/home/user_name/Downloads
User_prompt:~/Downloads$ 
muru
  • 193,181
  • 53
  • 473
  • 722
Daniel Oscar
  • 191
  • 7

1 Answers1

1

I don't think you can stop it from printing, since that's the documented behavior:

If a non-empty directory name from CDPATH is used, or if ‘-’ is the first argument, and the directory change is successful, the absolute pathname of the new working directory is written to the standard output.

However you could overload cd with a shell function that redirects standard output:

cd ()
{
    command cd "$@" > /dev/null
}
muru
  • 193,181
  • 53
  • 473
  • 722
steeldriver
  • 131,985
  • 21
  • 239
  • 326
  • I wrote a custom `cd` function for someone before: [How do I prevent 'cd' command from going to home directory?[](https://askubuntu.com/questions/1086161/how-do-i-prevent-cd-command-from-going-to-home-directory/1086196#1086196) and it was not well received. Some frown on writing functions that override built ins. – WinEunuuchs2Unix Feb 15 '21 at 06:03
  • 2
    Perhaps more "elegant": `alias cd='>/dev/null cd'` – vanadium Feb 15 '21 at 17:30
  • @vanadium this solved the problem. You may write a full answer if you will! – Daniel Oscar Feb 16 '21 at 01:05
  • @steeldriver, perhaps you care adding this to your answer? Your approach works equally well, but a solution where you just add one more alias is more elegant and straightforward. – vanadium Feb 16 '21 at 10:01
  • command cd ${1+"$@"} -- without that wrapper, the behavior changes. – Bruce Jun 27 '22 at 16:57