Home > Back-end >  How to add wrapper around existing command in bash?
How to add wrapper around existing command in bash?

Time:03-10

Is there any way to add to or wrap an existing command?

For example, if someone runs git pull it really runs git pull --rebase?

Sure, we've written an alias for it, but the issue is that people aren't using the alias.

Is there any way to detect that git pull was run and add to it?

An ideal solution?

$ git pull

echoes out

$ This is not the prefered way of pulling down new code. Did you mean to run ~/my_scripts/git_pull_script.sh?

And then cancels the original git pull.

There's probably a simpler way to do this kind of thing, but searching around hasn't yielded much on adapting existing commands.

CodePudding user response:

You can use the command substitution operator $(command) to run a command and capture its output.

For example, if you want to run a command and capture its output, you can use the following:

$(command)

This is useful if you want to run a command and capture its output.

CodePudding user response:

To concretise the answer by @Barmar you could write a script like this:

echo $1
if [ "$1" == "pull" ] ;then
    echo This is not the prefered way of pulling down new code. Did you mean to run ~/my_scripts/git_pull_script.sh?
else
    git $*
fi

name it git and place it in a directory, that is in the PATH variable of the users before the actual place of the git binary.

  • Related