Michael Trojanek
Michael Trojanek

Reputation: 1943

How to modify every bash command before execution?

Is there a way to modify every command that is entered at the bash prompt with a script/hook?

In my ideal world, the script would fire after the user has entered the command but BEFORE the Enter key is pressed. It would get the command string as a parameter, modifiy it and hand it over to bash for executing (so everything would happen transparently).

I would use this hook for some company-specific substitutions which cannot be done using aliases, but above all I'm interested if this can be done.

I know of some hacks to do something with the last command after it has been executed (trap 'function' DEBUG and the like) as there are a lot of questions concerning that scenario but this is of no help here.

Thanks and kind regards!

Upvotes: 5

Views: 1069

Answers (1)

jim mcnamara
jim mcnamara

Reputation: 16379

What you want is a kind of command completion -- it seems to me.

There is a lot behind bash line editing: bindable readline commands, or command completion and command substitution.

First off you can write write and compile your own bash builtins: http://www.drdobbs.com/shell-corner-bash-dynamically-loadable-b/199102950

Next, you can alter bash through what people call edit line or readline: Start here maybe: http://www.math.utah.edu/docs/info/features_7.html

http://www.gnu.org/software/bash/manual/html_node/Command-Line-Editing.html

Upvotes: 3

Related Questions