Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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
share|improve this answer
    
thanks, "command" is exactly what I'm looking for. It seems kind of obtuse to have to use for every system command executed though. I guess the only sure fire way to ensure you're running the system command is to specify the whole path. However, you're banking on the commands being in the same path across different distros at with that approach. –  acm Sep 17 '12 at 18:07
    
@acm As I wrote, you can also go the env route. Additionally, you can determine each tool's location once and store it in a variable, e.g. RM=$( /usr/bin/env which rm ); [much more code]; $RM some_file;. You could also change how your script is executed. Usually, functions and aliases aren't inherited, otherwise they'd break scripts all the time. –  Daniel Beck Sep 17 '12 at 18:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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