使用 PROMPT COMMAND envrionment 变量

当完成交互式 bash 实例中的最后一个命令时,将显示已评估的 PS1 变量。在实际显示 PS1 之前,bash 会查看是否设置了 PROMPT_COMMAND。此 var 的值必须是可调用的程序或脚本。如果设置了此 var,则在显示 PS1 提示之前调用此程序/脚本。

# just a stupid function, we will use to demonstrate
# we check the date if Hour is 12 and Minute is lower than 59
lunchbreak(){ 
   if (( $(date +%H) == 12 && $(date +%M) < 59 )); then 
      # and print colored \033[ starts the escape sequence 
      # 5; is blinking attribute
      # 2; means bold
      # 31 says red
      printf "\033[5;1;31mmind the lunch break\033[0m\n";
   else
      printf "\033[33mstill working...\033[0m\n"; 
   fi; 
}

# activating it
export PROMPT_COMMAND=lunchbreak