0

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.

1
  • This smells like an XY problem. What is it you're really trying to accomplish? Commented Apr 21, 2015 at 15:18

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No i need <my_command>to be executed before shell starts executing the command passed by user. PROMPT_COMMAND is executed by shell after executing the command passed by user. In above case before shell executes "pwd" it should execute "my_command"
PROMPT_COMMAND is executed when Bash displays a prompt which is before a user types a command.
0

My example

function preexec() {
  echo "Preexec command"
}

trap 'preexec' DEBUG

Result is

# pwd
Preexec command
/home/robert

2 Comments

I'm using this version of shell "GNU bash, version 2.04.0(1)-release",according to the man page of this version PROMPT_COMMAND is executed after User's input is executed.
Sorry, then. I think there is no way how to do it :(

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.