3

Is it possible to add an option to an existing Bash command?

For example I would like to run a shell script when I pass -foo to a specific command (cp, mkdir, rm...).

2
  • 2
    1) make shell scripts named cp, mkdir, rm, put them in a directory that comes as the first item in the PATH; 2) adjust the sources and recompile cp, mkdir, rm utilities. Out of curiosity, why do you need this? Commented Nov 1, 2012 at 10:18
  • Thanks khachik. I would like to add an option to mkdir which allows to enter in the new directory. Commented Nov 1, 2012 at 11:02

2 Answers 2

6

You can make an alias for e.g. cp which calls a special script that checks for your special arguments, and in turn call the special script:

$ alias cp="my-command-script cp $*"

And the script can look like

#!/bin/sh

# Get the actual command to be called
command="$1"
shift

# To save the real arguments
arguments=""

# Check for "-foo"
for arg in $*
do
    case $arg in
    -foo)
        # TODO: Call your "foo" script"
        ;;
    *)
        arguments="$arguments $arg"
        ;;
esac
done

# Now call the actual command
$command $arguments
Sign up to request clarification or add additional context in comments.

Comments

0

Some programmer dude's code may look cool and attractive... but you should use it very carefully for most commands: https://unix.stackexchange.com/questions/41571/what-is-the-difference-between-and

About usage of $* and $@:

You shouldn't use either of these, because they can break unexpectedly as soon as you have arguments containing spaces or wildcards.

I was using this myself for at least months until I realized it was the reason why my bash code sometimes didn't work.

Consider much more reliable, but less easy and less portable option. As pointed out in comments, recompile original command with changes, that is:

Download c/c++ source code from some respected developers repositories:

Add some code in c/c++, compile with gcc/g++.

Also, I guess, you can edit bash itself to set it to check if a string passed to bash as a command matches some pattern, don't execute this and execute some different command or a bash script

If you really are into this idea of customizing and adding functionality to your shell, maybe check out some other cool fashionable shells like zsh, fish, probably they have something, I don't know.

3 Comments

"When they are not quoted, $* and $@ are the same. You shouldn't use either of these... "$@" will work properly all the time.". Modifying sources is a ridiculously overcomplicated solution.
@galagora, haha. Agreed regarding modifying sources. Obviously.
@galagora You can try this: git_path=`command -v git`; function git() { if [[ $1 = "yourcustom_git_command" ]]; then; shift; git_myCustomCommand "$@"; else $git_path "$@"; fi; }; This is how I used to add git commands back in the days. I'm not putting this as an answer, because in this case, "$@" won't work properly for the OP. It works as per bash specs though. Shortly speaking, if you try this piece in practice and add it to say .bashrc, you will be running into troubles at some point. Specifically, when using wildcards/regexp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.