3

I came across this old post, which I find useful. Now, I'm trying hard, but in vain, to tell which part of it is responsible for printing the timestamp at the end of the dashes.

The following is the script for ease of reference:

# Fill with minuses
# (this is recalculated every time the prompt is shown in function prompt_command):
fill="—- "
reset_style='\[\033[00m\]'
status_style=$reset_style'\[\033[0;90m\]' # gray color; use 0;37m for lighter color
prompt_style=$reset_style
command_style=$reset_style'\[\033[1;29m\]' # bold black

# Prompt variable:
PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style "

# Reset color for command output
# (this one is invoked every time before a command is executed):
trap 'echo -ne "\e[0m"' DEBUG

function prompt_command {
    # create a $fill of all screen width minus the time string and a space:
    let fillsize=${COLUMNS}-9
    fill=""
    while [ "$fillsize" -gt "0" ]
    do
        fill="-${fill}" # fill with underscores to work on
        let fillsize=${fillsize}-1
    done

    # If this is an xterm set the title to user@host:dir
    case "$TERM" in
    xterm*|rxvt*)
        bname=`basename "${PWD/$HOME/~}"`
        echo -ne "\033]0;${bname}: ${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"
        ;;
    *)
        ;;
    esac
}

PROMPT_COMMAND=prompt_command

Any hint?

6

It's the \t in this line that composes the whole prompt, incorporating the variables (including fill which gives the dashes) that have been assigned:

PS1="$status_style"'$fill \t\n'"$prompt_style"'${debian_chroot:+($debian_chroot)}\u@\h:\w\$'"$command_style "

In PS1, \t is a Bash escape sequence that prints the current time.

Test:

zanna@toaster:~$ PS1="\t"
21:33:06
  • Thanks, bud! Never thought that \t, which I always see as a tab, would turn out to be time stamp! – joker Dec 20 '17 at 21:59
  • 2
    @joker ikr! Prompt codes are strange. Added a link to one of the many docs out there (Arch Wiki is awesome imho). – Zanna Dec 20 '17 at 22:15

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.