3

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
1
  • 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, 2016 at 0:35

1 Answer 1

5

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.

9
  • interesting, so i could make a command_not_found_handle() function that convert the command to lowercase.
    – eadmaster
    May 29, 2016 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, 2016 at 12:35
  • See my updated answer. It might be something useful.
    – Vombat
    May 31, 2016 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, 2016 at 6:01
  • Yes I know. I just wanted to have the shortest possible function name. Something else can be used instead.
    – Vombat
    Jun 1, 2016 at 6:05

You must log in to answer this question.

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