How to run some command before or after every Bash command entered from console?

后端 未结 2 1643
情书的邮戳
情书的邮戳 2020-11-29 06:03

I want to run a command, for example

echo \"foobar\";

After each command, entered by the user.

Two scenarios:

  • When th
相关标签:
2条回答
  • 2020-11-29 06:27

    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
    
    0 讨论(0)
  • 2020-11-29 06:28

    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).

    0 讨论(0)
提交回复
热议问题