Bash case-insensitive commands matching

bashcase sensitivitycommand line

Is it possible for bash to find commands in a case-insensitive way?

eg. these command lines will always run python:

python
Python
PYTHON
pyThoN

Best Answer

One way is to use alias shell builtin, for example:

alias Python='python'
alias PYTHON='python'
alias Python='python'
alias pyThoN='python'

For a better approach, the command_not_found_handle() function can be used as described in this post: regex in alias. For instance, this will force all the commands to lowercase:

command_not_found_handle() {
    LOWERCASE_CMD=$(echo "$1" | tr '[A-Z]' '[a-z]')
    shift
    command -p $LOWERCASE_CMD "$@"
    return $?
}

Unfortunately it does not work with builtin commands like cd.

Also (if you have Bash 4.0) you can add a tiny function in your .bashrc to convert uppercase commands to lowercase before executing them. Something similar to this:

function :() {
    "${1,,}"
}

Then you can run the command by calling : Python in command line.

NB as @cas mentioned in the comments, : is a reserved bash word. So to avoid inconsistencies and issues you can replace it with c or something not already reserved.