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
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
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.
command_not_found_handle() function that convert the command to lowercase.
cd... btw still better than defining a lot of aliases...
: 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).