On 2006-10-12 @ 19:10:33 (week 41) José Alburquerque wrote: > Hi all. I'm wondering if anyone knows how I might be able to execute > the same command just before the execution of a command issued at the > prompt of a bash shell. > > Currently, I have my bash prompt set up so that it displays the current > date. If I can print the date just before each command is executed, I > can always be able to get a sense of how long each command has taken. > > Anyone has any idea how I might go about printing the date just before > the execution of each bash command? I just don't know where I would > start. Thanks for any help.
You could use PROMPT_COMMAND for this. I do to create a sort of statusbar in my shells (with the added help of tput). I've copied the relevant parts of the code I wrote for that below. Just add it to your bashrc, adjust the variables and case selectors in the "settings" section to your own tastes and try if that's what you're looking for. HTH Grx HdV #!/bin/bash ################################################################################ # Global variables ################################################################################ # Paths to external commands TTY=/usr/bin/tty TPUT=/usr/bin/tput ################################################################################ # Global functions ################################################################################ # Define text attributes function define_text_attributes { ${TPUT} init RESET="$(${TPUT} sgr0)" BOLD="$(${TPUT} bold 1)" DIM="$(${TPUT} dim)" BLINK="$(${TPUT} blink 1)" REVERSE="$(${TPUT} rev 1)" STRIKETHROUGH="$(${TPUT} os 1)" UNDERLINE="$(${TPUT} ul 1)" BLACK="$(${TPUT} setaf 0)" BLACKBG="$(${TPUT} setab 0)" DARKGREY="$(${TPUT} bold ; ${TPUT} setaf 0)" LIGHTGREY="$(${TPUT} setaf 7)" LIGHTGREYBG="$(${TPUT} setab 7)" WHITE="$(${TPUT} bold ; ${TPUT} setaf 7)" RED="$(${TPUT} setaf 1)" LIGHTRED="$(${TPUT} bold ; ${TPUT} setaf 1)" REDBG="$(${TPUT} setab 1)" GREEN="$(${TPUT} setaf 2)" LIGHTGREEN="$(${TPUT} bold ; ${TPUT} setaf 2)" GREENBG="$(${TPUT} setab 2)" BROWN="$(${TPUT} setaf 3)" BROWNBG="$(${TPUT} setab 3)" YELLOW="$(${TPUT} bold ; ${TPUT} setaf 3)" BLUE="$(${TPUT} setaf 4)" LIGHTBLUE="$(${TPUT} bold ; ${TPUT} setaf 4)" BLUEBG="$(${TPUT} setab 4)" PURPLE="$(${TPUT} setaf 5)" PURPLEBG="$(${TPUT} setab 5)" PINK="$(${TPUT} bold ; ${TPUT} setaf 5)" CYAN="$(${TPUT} setaf 6)" LIGHTCYAN="$(${TPUT} bold ; ${TPUT} setaf 6)" CYANBG="$(${TPUT} setab 6)" } # Build and print the statusbar function print_statusbar { TTY_LBL="" PWD_LBL="" PADDING="" DT_LBL="" case "${DELIMITER}" in \[\] | \{\} | \<\> | \(\) ) DEL_OPEN=${DELIMITER:0:1} DEL_CLOSE=${DELIMITER:1:1} ;; * ) DEL_OPEN='' DEL_CLOSE=${DELIMITER} ;; esac if [ "${SHOW_TTY}" = "1" ]; then TTY_NAME=$( ${TTY} ) if [ -n "${TTY_NAME}" ]; then TTY_LBL=${TTY_NAME#/dev/} else TTY_LBL="unknown" fi if [ -n "${DEL_OPEN}" ]; then TTY_LBL="${DEL_OPEN}${TTY_LBL}${DEL_CLOSE}" elif [ "${SHOW_PWD}" = "1" ]; then TTY_LBL="${DEL_OPEN}${TTY_LBL}${DEL_CLOSE}" fi fi if [ "${SHOW_PWD}" = "1" ]; then if [ "${SHOW_DATETIME}" = "1" ]; then PWD_LBL="${DEL_OPEN}${PWD}${DEL_CLOSE}" elif [ -n "${DEL_OPEN}" ]; then PWD_LBL="${DEL_OPEN}${PWD}${DEL_CLOSE}" else PWD_LBL=${PWD} fi fi if [ "${SHOW_DATETIME}" = "1" ]; then DT_LBL=$( date "+${DATETIME_FMT}" ) if [ -n "${DEL_OPEN}" ]; then DT_LBL="${DEL_OPEN}${DT_LBL}${DEL_CLOSE}" fi fi SPACES=$(( $( ${TPUT} cols ) - ${#TTY_LBL} - ${#PWD_LBL} - ${#DT_LBL} )) if [ ${SPACES} -lt 0 ]; then if [ "${CLIP_PWDTAIL}" = "1" ]; then PWD_LBL=${PWD:0:$(( ${#PWD} + ${SPACES} - ${#CLIP_STR} ))} PWD_LBL="${PWD_LBL}${CLIP_STR}" else PWD_LBL=${PWD:$(( ${#CLIP_STR} - ${SPACES} ))} PWD_LBL="${CLIP_STR}${PWD_LBL}" fi if [ "${SHOW_DATETIME}" = "1" ]; then PWD_LBL="${DEL_OPEN}${PWD_LBL}${DEL_CLOSE}" elif [ -n "${DEL_OPEN}" ]; then PWD_LBL="${DEL_OPEN}${PWD_LBL}${DEL_CLOSE}" fi else while [ ${#PADDING} -lt ${SPACES} ]; do PADDING="${PADDING} " done if [ "${SHOW_PWD}" = "1" ]; then PWD_LBL="${PWD}${PADDING}" if [ "${SHOW_DATETIME}" = "1" ]; then PWD_LBL="${DEL_OPEN}${PWD_LBL}${DEL_CLOSE}" elif [ -n "${DEL_OPEN}" ]; then PWD_LBL="${DEL_OPEN}${PWD_LBL}${DEL_CLOSE}" fi else PWD_LBL=${PADDING} fi fi LABEL="${STATUSBG}${STATUSFG}${TTY_LBL}${PWD_LBL}${DT_LBL}${RESET}" if [ "${STATUS_TOP}" = "1" ]; then #ToDo: fix error where prompt overwrites statusbar when on first line ${TPUT} sc ${TPUT} cup 0 0 ${TPUT} el echo "${LABEL}" ${TPUT} rc else echo "${LABEL}" fi } ################################################################################ # Settings for interactive shells ################################################################################ # If running interactively, then: if [ "$PS1" ]; then # Variables defining properties of the statusbar STATUS_TOP=1 # Show status on top instead of before prompt SHOW_TTY=1 # Show name of current terminal SHOW_PWD=1 # Show name of current working directory SHOW_DATETIME=1 # Show current datetime DATETIME_FMT="%a %F %T][%V/%j" # Show datetime using this format string CLIP_PWDTAIL=0 # Clip name of PWD at head or tail CLIP_STR="..." # String that shows PWD was clipped DELIMITER="[]" # Delimiter to put between statusbar elements SHOW_HISTORYNR=0 # Show history number of command in prompt # Set prompt and statusbar attributes per user/host combination define_text_attributes WHO="$( whoami )@$( hostname )" case "${WHO}" in [EMAIL PROTECTED] ) STATUSFG=${YELLOW} STATUSBG=${BLUEBG} PROMPTFG=${YELLOW} PROMPTBG= ;; [EMAIL PROTECTED] ) STATUSFG=${YELLOW} STATUSBG=${REDBG} PROMPTFG=${RED} PROMPTBG= ;; * ) STATUSFG=${LIGHTCYAN} STATUSBG=${BLUEBG} PROMPTFG=${LIGHTCYAN} PROMPTBG= ;; esac if [ "${SHOW_HISTORYNR}" = "1" ]; then PS1="[EMAIL PROTECTED]: \[${RESET}\]" else PS1="[EMAIL PROTECTED]: \[${RESET}\]" fi PROMPT_COMMAND=print_statusbar if ################################################################################ # Handy support function to find out what combinations you like best ################################################################################ # Print a list of available text attributes function show_text_attributes { echo ${BOLD}BOLD${RESET} echo ${DIM}DIM${RESET} echo ${BLINK}BLINK${RESET} echo ${REVERSE}REVERSE${RESET} echo ${STRIKETHROUGH}STRIKETHROUGH${RESET} echo ${UNDERLINE}UNDERLINE${RESET} echo ${BLACK}BLACK${RESET} echo ${BLACKBG}BLACKBG${RESET} echo ${DARKGREY}DARKGREY${RESET} echo ${LIGHTGREY}LIGHTGREY${RESET} echo ${LIGHTGREYBG}LIGHTGREYBG${RESET} echo ${WHITE}WHITE${RESET} echo ${RED}RED${RESET} echo ${LIGHTRED}LIGHTRED${RESET} echo ${REDBG}REDBG${RESET} echo ${GREEN}GREEN${RESET} echo ${LIGHTGREEN}LIGHTGREEN${RESET} echo ${GREENBG}GREENBG${RESET} echo ${BROWN}BROWN${RESET} echo ${BROWNBG}BROWNBG${RESET} echo ${YELLOW}YELLOW${RESET} echo ${BLUE}BLUE${RESET} echo ${LIGHTBLUE}LIGHTBLUE${RESET} echo ${BLUEBG}BLUEBG${RESET} echo ${PURPLE}PURPLE${RESET} echo ${PURPLEBG}PURPLEBG${RESET} echo ${PINK}PINK${RESET} echo ${CYAN}CYAN${RESET} echo ${LIGHTCYAN}LIGHTCYAN${RESET} echo ${CYANBG}CYANBG${RESET} }