bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PS1 prompt problem


From: Chet Ramey
Subject: Re: PS1 prompt problem
Date: Fri, 16 Oct 2009 15:18:04 -0400
User-agent: Thunderbird 2.0.0.23 (Macintosh/20090812)

Nils wrote:
> I'm using bash 3.2 and I'd like to set my prompt to the following
> (inspired by the Opensolaris prompt for ksh93):
> 
> PS1='$(
>     spwd="${PWD/#${HOME}/~}"
>     [[ ${#spwd} -gt 10 ]] && spwd="...${spwd: -10}"
>     printf "%s@%s:%s%s " "\u" "\h" "${spwd}" "\$"
>     termtitle="\u@\h:${spwd}"
> 
>     ####
>     printf "%s" "\["
>     case "${TERM}" in
>         (xterm*)
>             printf "\033]0;%s\a" "${termtitle}"
>             ;;
>         (screen*)
>             printf "\033_%s\033\\" "${termtitle}"
>             ;;
>     esac
>     printf "%s" "\]"
>     ####
> )'
> 
> Basically this is supposed to shorten PWD to the last 10 characters
> and set the terminal title as well. Unfortunately it causes bash to
> hang, the problem seems to lie in the escape codes for entering and
> leaving the terminal title and the surrounding "\[" "\]" because
> commenting out the case statement and the surrounding printfs makes it
> at least set the prompt correctly. I have the strong suspicion it
> might be some quoting issue.

Sort of.  I think you're overlooking the various expansion (and backslash
escaping) that's taking place with your prompt.

Since you set the value of PS1 to literal string containing a command
substitution, the value will be expanded twice before being sent to
readline.  The first expansion is the normal set of backslash escapes
performed by prompt string decoding; the second is the set of shell word
expansions, including command substitution.

This is the problem line: printf "\033_%s\033\\" "${termtitle}".  I
assume the intent is to wind up with a string ending with a single
backslash written to the terminal.  The string as written results in
a parsing error.

One of the prompt string decoding expansions turns \\ into \, so the
form of the above string when passed to the parser by the command
substitution is

        printf "ESC_%sESC\" "${termtitle}"

(the ESC stands for a literal escape character).  The escaped double quote
is probably not what's intended.

For printf to see what you want it to, you need to double the backslashes
in that string:

        printf "\033_%s\033\\\\" "${termtitle}"

Try that and see what you get.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]