user3446120
user3446120

Reputation: 77

How to make shell execute a command prior to every user input command

I need a command to be executed before shell starts to execute the user passed command,i tried using trap with DEBUG signal, but that did not happen.

I have registered trap in /etc/profile.

trap 'echo "my_command"' DEBUG

Whats happening now is:

sw0:root:root> pwd

/root

my_command

sw0:root:root>

What i want is:

sw0:root:root> pwd

my_command

/root

sw0:root:root>

Bash Version Used:

GNU bash, version 2.04.0(1)-release (powerpc-unknown-linux-gnu)

I want my command to be executed prior to every command entered by user in shell,how do i do that?

Please help me on this,i tried lot of googling but that does not help.

Upvotes: 0

Views: 241

Answers (2)

Robert Vojcik
Robert Vojcik

Reputation: 1

My example

function preexec() {
  echo "Preexec command"
}

trap 'preexec' DEBUG

Result is

# pwd
Preexec command
/home/robert

Upvotes: 0

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136525

You are probably looking for something like PROMPT_COMMAND:

The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

Upvotes: 1

Related Questions