everyone. I am a bash/zsh user(Linux). I want to realize the following things.

First, input the command in the bash/zsh shell, like:

$ wonderful:

In fact, a program named `wonderful:' doesn't exists. What I want is to deal with the input-string with a function(or program) before the shell executes the command. More specifically, when the command input is `wonderful:', the shell deliver the input to my function/program, and that's all the shell does.

Does there exists a way to realize such a thing? Either bash or zsh is ok. Thanks a lot for your attention!

closed as not a real question by Dave, Dennis, Nifle, Renan, 3498DB Mar 15 '13 at 16:30

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    I'm not sure I understand what you're trying to do. Does piping cat to your program achieve what you want? – Dennis Mar 15 '13 at 12:43

You can define a function called command_not_found_handle which will be invoked any time an entered command is not found. This is supported in bash 4.0 or later; the equivalent zsh function is the similarly named command_not_found_handler.

A simple example that intercepts a misspelling of printf, but leaves any other unknown command as unknown.

command_not_found_handle () {
    if [[ $1 == pirntf ]]; then
        printf "${@:2}"
       printf "Command not found: $1"
       return 127
  • i also thought about suggesting that, but he wants to handle the command 'before' it exexutes – weberik Mar 15 '13 at 13:42
  • That's the point of command_not_found_handle. Without it, nothing executes because there was no command found to execute. The function takes the invalid command line as input, and allows you to pick it apart, passing the arguments to another known command. – chepner Mar 15 '13 at 13:45
  • 1
    In fact, the function command_not_found_handle works in bash, but it seems that only command_not_found_handler works for zsh. – Libin Wen Mar 16 '13 at 7:41

Maybe what you need is a Bash alias. An alias allows would allow you to define 'wonderful' as if it were a command. See the tutorial. For example, you can add:

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# -> Prevents accidentally clobbering files.
alias mkdir='mkdir -p'

to your ~/.bashrc file.

In your case you can alias "wonderful" to a script that handles the parameters or input, then invokes some other command.

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