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

eg. these command lines will always run python:

  • 2
    2^6 aliases for python. 2^4 for bash. 2^(len(filename)) for how many other programs? and then copy those aliases to every other system you use. I recommend just accepting the fact that unix filesystems are case-sensitive and learning to type (with tab-completion) the correct command name. some things aren't problems and don't need to be solved. – cas May 30 '16 at 0:35

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]')
    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 :() {

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.

  • interesting, so i could make a command_not_found_handle() function that convert the command to lowercase. – eadmaster May 29 '16 at 14:13
  • 1
    update: it works, but not with bultin commands like cd... btw still better than defining a lot of aliases... – eadmaster May 31 '16 at 12:35
  • See my updated answer. It might be something useful. – coffeMug May 31 '16 at 17:26
  • : is a shell builtin, the null statement. It does nothing, and does so quite deliberately. It's useful as a placeholder in, e.g., if and similar statements: if something; then : else echo something ; fi, which would otherwise be a syntax error. see man bash and search for ` : ` (space,colon,space). – cas Jun 1 '16 at 6:01
  • Yes I know. I just wanted to have the shortest possible function name. Something else can be used instead. – coffeMug Jun 1 '16 at 6:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.