1

PROMPT_COMMAND

If set, the value is executed as a command prior to issuing each primary prompt.

We have shell variable PROMPT_COMMAND to specify the command to run before prompt shows up, namely, just right after the execution of the command we typed in.

But I didn't find the counterpart which I expect to let us run a command right after we hit enter but before the execution of the command we typed in.

Is there any way to do this?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
alzee
  • 463
  • 2
  • 10

2 Answers2

2

As @storm identified there is indeed a way to do this, for the specific functionality you mentioned in your question. See storm's answer.

It seems that what you're looking for is a set of "hooks" to allow you to add functionality to different parts of the shell. As fun as it sounds, bash doesn't have much of that.

The purpose of PROMPT_COMMAND is generally considered to be to set up stuff that will be used by your prompt. This SO question has some answers that demonstrate how it might be used for this. Of course, you can use it for other things (it's a hook after all), but the fact that it gets executed before your prompt kind of relates it to your prompt.

If you decide to trap for DEBUG, you should be aware that the trap doesn't follow you into functions, but a trap set within a function follows you back into the calling shell:

$ function red { trap "echo world" DEBUG; echo "RED"; echo "RED"; }
$ function blue { echo "BLUE"; echo "BLUE"; }
$ trap "echo hello" DEBUG
$ red
hello
world
RED
world
RED
$ blue
world
BLUE
BLUE

If you want a trap like this to be local to the function, you must define the function as a subshell, for instance:

$ red() ( trap "echo world" DEBUG; echo "RED"; echo "RED" )

which is equivalent to:

$ function red { ( trap "echo world" DEBUG; echo "RED"; echo "RED" ); }

I don't see this trap-within-a-function behaviour documented anywhere, and it seems a little approximate to me. I wouldn't be surprised if it changed in future versions of bash.

I can't speak for Chet, but I doubt we'll be seeing much hook functionality get added to bash 4. No idea what new functionality might be in the works for future versions.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • ++ for functions example – storm Apr 13 '18 at 07:43
  • Yes, 'trap set within a function follows you back into the calling shell' just confused me. You explained well, thanks a lot. – alzee Apr 13 '18 at 07:53
  • Since @storm gave the idea, I would like to give the credit to him. However, I appreciate your answer. It's very helpful. – alzee Apr 13 '18 at 07:59
2

You can use the trap command , from man trap

If a SIGNAL_SPEC is DEBUG, ARG is executed before every simple command.

The idea is pretty straightforward: if you run :

trap "echo command" DEBUG

Then Bash will run echo command before it executes each subsequent command

~$ pwd
command
/home
~$ echo Bye
command
Bye

More details here.

storm
  • 379
  • 1
  • 9
  • 23
  • DOH! You win. I'll delete my answer. :-) – ghoti Apr 13 '18 at 07:10
  • @ghoti, no, I've read you answer ,and I like it. Would you re-post it here, of course you can make some change. – alzee Apr 13 '18 at 07:14
  • I agree with @dotc your answer seems to be helpful for OP you can edit it and add exemples , I'll test the command and edit my answer too when i find some time. – storm Apr 13 '18 at 07:24