Ubuntu – Run the bash script every time when command lines are executed

bashcommand lineexecute-commandscripts

I am a new Ubuntu/Linux user, I would like to ask a question as my title above.

For example, I have a bash file ./script, I would like to make it run automatically when I type any command line. Once I execute command line pwd then the ./script will be executed. Once I type the next command line ls -la then the ./script will be executed again.

Best Answer

You need to set PROMPT_COMMAND variable. From man bash:

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

For example, this will write current date into /tmp/PC every time a command is executed:

 $ PROMPT_COMMAND="date > /tmp/PC"
Related Question