29

I want to run a command, for example

echo "foobar";

After each command, entered by the user.

Two scenarios:

  • When the user enters a command, my global command should be executed, and later his command should be executed
  • When the user enters a command, his command should be executed, and later my global command should be executed

How to accomplish the above two scenarios?

NB: I don't want to use the prompt for this purpose, (leave the PS1 variable as is).

codeforester
  • 39,467
  • 16
  • 112
  • 140
astropanic
  • 10,800
  • 19
  • 72
  • 132

2 Answers2

17

As l0b0 suggests, you can use PROMPT_COMMAND to do your second request and you won't have to touch PS1.

To do your first request, you can trap the DEBUG pseudo-signal:

trap 'echo "foobar"' DEBUG
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 1
    FOr me using the trap trick executes the code one time before and four times after the command. – trusktr Oct 04 '12 at 10:28
  • @trusktr: That probably means that you have `PROMPT_COMMAND` set to run some commands. Unfortunately, because the `DEBUG` trap executes before commands are run, you can't turn the trap off at the beginning of your `PROMPT_COMMAND` and back off at the end because the command to turn it off will trigger it. – Dennis Williamson Oct 04 '12 at 14:22
  • Yikes. That's ugly. I'm trying to implement something so that my Bash history is synchronized between terminals, but no matter what I do, I can't make it happen. I thought maybe if there's a way to implement a pre-command feature (like this trap example) then I could force the history to refresh based on the other terminal's last command. So far, I've found various methods that use PROMPT_COMMAND to write to HISTFILE then refresh the history, but this doesn't get reflected in other terminal until at least one command gets executed (at least pressing enter one time). – trusktr Oct 05 '12 at 22:23
  • Zsh would make it easy to implement this, but I don't want to switch to Zsh just yet until I know what I'm doing so I don't break system functionality. – trusktr Oct 05 '12 at 22:24
  • @trusktr: Search on SuperUser, Unix/Linux and AskUbuntu. On at lease one of those I've seen questions regarding syncing Bash history. – Dennis Williamson Oct 06 '12 at 00:30
  • 1
    @trusktr : for example, http://unix.stackexchange.com/questions/48111/real-time-history-export-amongst-bash-terminal-windows – michael Oct 14 '12 at 07:04
10

For the second part you could use declare -r PROMPT_COMMAND="echo 'foobar'": It is executed just before the prompt is displayed. Beware that it will not be run for each command in for example a pipe or command group.

Beware that any solution to this has the potential to mess things up for the user, so you should ideally only call commands which do not output anything (otherwise any output handling is virtually impossible) and which are not available to the user (to avoid them faking or corrupting the output).

Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
l0b0
  • 55,365
  • 30
  • 138
  • 223