I have a task that is assisting my scripting...it requires counting the amount of times I use each user defined command in .bash_profile. In other words, if I run on command like something defined as an alias like pip2install arg1 arg2, I need to count that I ran pip2install.

Is there a way in linux machines to run a command before any shell script/alias/bash function runs? Like a config file where this can be defined?

I'm OK with counting how many times I run ALL commands (since I can just choose to ignore builtin commands once the UI is built)

Thank you very much


In Bash You can set the PROMPT_COMMAND environment variable to a command that is executed before displaying the command prompt (so, at the end of the execution of the previous command, for synchronous commands). See here. For instance try:

export PROMPT_COMMAND="echo This is displayed before your prompt"
  • so from here I just parse the last history command and run the script to write to db? – codyc4321 Jun 28 '17 at 14:24
  • Yes, that's a possibility. But if you just rely on bash_history, you can just use that file once at the end of the day.... (after closing all the terminal windows). – xenoid Jun 28 '17 at 15:20

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.