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.

  • 4
    may we ask : what kind of action you are intending to do on each and every command typed? and should it be only for those typed interactively? or also for any command in a script? and what about commands typed over ssh ? etc. This could be a XYProblem Oct 3, 2016 at 14:05

2 Answers 2


You need to set PROMPT_COMMAND variable. From man bash:

              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"
  • 1
    Yep, I got it.. pretty cool solution. ( upvoted)
    – Ender Phan
    Oct 3, 2016 at 11:22
  • 1
    This is wrong. The command echo $(date) will be run at declaration time, so will always show (save) the same time.
    – heemayl
    Oct 3, 2016 at 11:27
  • @heemayl: of course you're right, I fixed it Oct 3, 2016 at 11:30
  • @lộcphan If one of these answers solved your issue, please take a moment and accept it by clicking on the check mark to the left. That will mark the question as answered and is the way thanks are expressed on the Stack Exchange sites.
    – terdon
    Oct 3, 2016 at 11:36
  • @heemayl: Why? ... Oct 3, 2016 at 11:38

You are looking for PROMPT_COMMAND environment variable, the value of this variable will be executed as command, after each command given in the terminal is executed, just before showing the next prompt.

For example, set it as:

export PROMPT_COMMAND='/path/to/script'

Make the script, /path/to/script, executable first.

Now the script will be run after each command.

Just to note, if you don't want the variable to be exported i.e. make it a shell variable instead of a environment one, drop export:


From man bash:

If set, the value is executed as a command prior to issuing each primary prompt.
  • I got it, thank you so much for your help :) ( upvoted btw )
    – Ender Phan
    Oct 3, 2016 at 11:22
  • Why are you using export? Oct 3, 2016 at 11:23
  • @ArkadiuszDrabczyk export will make the variable PROMPT_COMMAND an environment variable i.e. the variable will be available to all child processes (e.g. subshells) of the current shell. If you just want to confine it within current shell session only (shell variable), remove export.
    – heemayl
    Oct 3, 2016 at 11:24
  • 5
    @heemayl: IMO export is not a good practice here - PROMPT_COMMAND should only be set in an interactive shell startup file and should not be visible in environment Oct 3, 2016 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.