23

Is it possible to call a script in this example named hey.sh after every bash command?

For example you type ls hit enter and ls runs, then hey.sh would run.

You then type cd .. (or any other command), cd runs then hey.sh would run, and so on.

2
  • 2
    This is either an X-Y problem or a very odd idea; what are you trying to actually achieve?
    – jasonwryan
    Jan 14, 2016 at 18:54
  • alias in bash.rc maybe?
    – moonbutt74
    Jan 14, 2016 at 18:56

1 Answer 1

31
PROMPT_COMMAND+="hey.sh;"

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


Note: Environment variables vs shell variables

By default, PROMPT_COMMAND is not an environment variable. It's just a shell variable.

Both types of variables are accessed the same way ("$variable*"), but environment variables are inherited by child processes whereas shell variables aren't.

The convention is to capitalize environment variables + shell variables that configure your shell (PROMPT_COMMAND does configure your shell), but keep other types of variables lower case.

In bash, you can do: declare -p to find out whether a variable is exported (=is an environment variable) or not:

$ declare -p PATH
declare -x PATH=... #-x means it's exported
$ declare PROMPT_COMMAND
declare -- PROMPT_COMMAND=... #no -x so just a shell variable
6
  • 2
    @John No. Don't export it. Just set it (or append to it, e.g., as shown above). Jan 14, 2016 at 19:07
  • Neat, what about after though?
    – moonbutt74
    Jan 14, 2016 at 19:42
  • 2
    @moonbutt74 prior to every command == after every command + at the beginning of your bash session Jan 14, 2016 at 19:50
  • @PSkocik I gotcha I was just curious if that was already an environmental variable, upon a quick echo command I found out it was. This does exactly what I was looking for
    – John
    Jan 15, 2016 at 14:48
  • @John By default, it's just a shell variable not an environment variable. Jan 15, 2016 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .