overriding user defined functions with the same name as system commands
This function exists in one of my users' ~/.bashrc:
function rm()
{
ls $*
echo "rm?"
read ans
if [ "$ans" == 'y' ]; then
/bin/rm $*
fi
}
In my ksh script that users are required to run, I have a line like this:
[[ "$KEEP" = "" ]] && \rm $FILE
While the backslash escapes user-defined aliases, it does not stop the script from running user defined functions with the same name. As a result, my user's rm() function is called instead of the system function.
I found this superuser question question & response, but the resolution only applies to a builtin function, not a system command.
What's the best to enforce the rm command from being called, and not an alias or function? Must I specify the full path to rm and every system command that I want to ensure is executed correctly? Is there a better way?
You can use command
to circumvent the normal bash function lookup.
command rm
Non-destructive example:
$ alias which='which -s'
$ function which { echo "which $@?" ; }
$ which which
which -s which?
$ command which which
/usr/bin/which
Alternatively, call it using env
(executing the first program with the given name on the $PATH
, or by specifying the full path.
/usr/bin/env rm
/bin/rm