Q: How do I replace a system command including its arguments on a Linux system?

Scenario: there is a command on a system which takes arguments.


$ command -a restart
Output: something

I'd like to replace the execution of that specific command with that specific parameter with e.g. command -a reload && echo hi. In that way, the command would never execute a restart, but a reload.


  • Other parameters should not interfere. command -a else should be executed as command -a else.
  • Command is executed for the root user only
  • I cannot replace the command file itself, as an update of the package would replace it

Is this possible?

  • what if it is e.g. command -b something -a restart ?
    – pLumo
    Jan 29 at 11:04
  • that won't work.
    – Kevin C
    Jan 29 at 11:05
  • What does that mean? Should it print an error? Should it run command -b something -a reload instead? Do you mean that the specific command you are thinking of doesn't allow more than one option to be passed? Also, who should this work for? Anyone on the system who runs command? Only one specific user?
    – terdon
    Jan 29 at 11:06
  • 1
    Write a shell script named command, which checks if options are -a restart. If yes, it does its custom thing, if no, it calls the original command and passes all options to it. You move the original command to an obscure directory and the shell script into a directory in PATH. Jan 29 at 11:21
  • 2
    @KevinC of course you can, you put it another directory in $PATH which comes before the directory where this command is. Typically programs from packages go in /usr/bin or /bin; and /usr/local/bin is very commonly used for overriding programs in those.
    – muru
    Jan 29 at 11:35

Add the following to your .bashrc or .bash_aliases file:

    if [ "$1 $2" = "-a restart" ] ; then
        shift 2
        my_command -a reload "$@"
        my_command "$@"
alias my_command="my_command_"

This will also change

my_command -a restart -b something
my_command -a reload -b something.

But it will not change my_command -b something -a restart.

System-wide alternative:

Create a script in another directory in $PATH which comes before the directory where this command is and make it executable, e.g. /usr/local/bin/my_command, with following content:

if [ "$1 $2" = "-a restart" ] ; then
    shift 2
    /usr/bin/my_command -a reload "$@"
    /usr/bin/my_command "$@"

Change /usr/bin/my_command to the actual path.

  • Sorry for my previous comment, I didn't copy and edited it properly on my system. This works like a charm!
    – Kevin C
    Jan 29 at 11:31
  • glad that it works :-)
    – pLumo
    Jan 29 at 11:32
  • Could this also work if it were to be system wide?
    – Kevin C
    Jan 29 at 11:36
  • yes, do what @muru commented. I added that option to my answer as well.
    – pLumo
    Jan 29 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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