使用 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