32

For example:

$ ls -aF
./ ../ bin/
$ cd tin # with a tee, not bee
bin
$ pwd
/home/user/bin

In other words, cd guesses that what I really meant was cd bin, and successfully (huh?) changes the current directory accordingly. I do not find this behavior documented in man bash or the Bash Reference Manual.

I would like Bash to produce an error, write something informative to standard error, and leave the current directory unchanged if no directory matching the dir argument (accounting for expansion) is found.

FYI,

$ type cd
cd is a shell builtin
$ ps -p $$
PID TTY          TIME CMD
46959 pts/8    00:00:00 bash
$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 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.
$ hostnamectl
   Static hostname:
         Icon name: computer-server
           Chassis: server
        Machine ID:
           Boot ID: 
  Operating System: Red Hat Enterprise Linux
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
            Kernel: Linux 3.10.0-1160.83.1.el7.x86_64
      Architecture: x86-64
$ #DELETED: Static hostname, Machine ID, Boot ID
0

1 Answer 1

45

The behavior of the cd command is affected by shopt. See man bash, search: shopt. See also the The Shopt Builtin in the Bash Reference Manual.

In particular, the behavior illustrated in the question is consistent with shopt -s cdspell.

I suppose clearing all options affecting cd would have the desired (per the question) effect. See the description of cd among the Bourne Shell Builtins or under "SHELL BUILTIN COMMANDS" in man bash.

Acknowledgement

Credit to an unnamed coworker for pointing out our system's shopt setting of cdspell, also acknowledged in an earlier comment.

3
  • 3
    Also look up CDPATH in the shell man page.
    – Ed Morton
    Feb 7, 2023 at 1:52
  • Shells like tcsh or zsh will have better ways to offer spelling corrections, either with dedicated configurable prompts when you enter commands or via the completion system. Feb 7, 2023 at 14:31
  • 4
    ObJargonFile: DWIM. Feb 7, 2023 at 19:12

You must log in to answer this question.

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