Well, you can do it, but it's not as easy as you think.
First you need to create a function that will check all directories in the PATH, and look there for the command you are trying to run. Then you need to associate this function with the DEBUG trap of your current shell.
I wrote a small script that does this:
$ cat /tmp/1.sh check_command() { n=0 DIRS="" for i in $(echo $PATH| tr : " ") do if [ -x "$i/$1" ] then n=$[n+1] DIRS="$DIRS $i" fi done if [ "$n" -gt 1 ] then echo "Warning: there are multiple commands in different PATH directories: "$DIRS fi } preexec () { check_command $1 } preexec_invoke_exec () { [ -n "$COMP_LINE" ] && return
Usage example:
$ . /tmp/1.sh $ sudo cp /bin/date /usr/bin/test_it $ sudo cp /bin/date /bin/test_it $ test_it Warning: there are multiple commands in different PATH directories: /usr/bin /bin Wed Jul 11 15:14:43 CEST 2012 $
source share