Is it possible to avoid executing a specific command?

I need to avoid executing a specific command in a bash script.
I was thinking of using the preexec trap for this.

Say I want to avoid the 'source' command for axample only. What I did is basically the following:

#!/bin/bash

function preexec ()
{
    if test $( echo "$BASH_COMMAND" | cut -d " " -f1 ) == "source"
        then
            echo ">>> do not execute this"
        else
            echo ">>> execute this"

    fi
}
trap 'preexec' DEBUG

echo "start"
source "source.sh"
echo "go on"

exit 0

the idea works fine, but at the moment I don’t know how to avoid executing the specified command.
Any idea how to solve this?

+5
source share
2 answers

A workaround would be to define an alias for this command, which does nothing and will not detect it after the script completes. An alias must be declared in the script itself for this:

alias source=:
## The actual script source here...
unalias source
+2
source

source, source.

.

source() { builtin source /dev/null; return 0; }
source() { read < /dev/null; return 0; }
source() { :; return 0; }
export -f source
+1

All Articles