another problem with bash PS1 handling

View: New views
4 Messages — Rating Filter:   Alert me  

another problem with bash PS1 handling

by Nils-20 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

PS1='$( [ "${LOGNAME}" != root ] && color=green )'

works in ksh93 and POSIX shell but fails in bash as bash seems to
expand the "!" to the next history file number before doing command
substitution. Setting PS1='$( [ "${LOGNAME}" !!= root ] &&
color=green )' preserves the "!" and works in bash but fails in ksh93
and POSIX shell.

Re: another problem with bash PS1 handling

by Chet Ramey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Nils wrote:
> PS1='$( [ "${LOGNAME}" != root ] && color=green )'
>
> works in ksh93 and POSIX shell but fails in bash as bash seems to
> expand the "!" to the next history file number before doing command
> substitution. Setting PS1='$( [ "${LOGNAME}" !!= root ] &&
> color=green )' preserves the "!" and works in bash but fails in ksh93
> and POSIX shell.

There are several issues at play here.  First, this only occurs when
bash is in posix mode, since that's the only time it attempts to do
the posix-style history substitution in the prompt strings.

Second, there are a couple of problems with Posix and this construct.
You can make an argument that Posix doesn't apply, since it only
calls for parameter expansion on the value of PS1, and that does not
include command substitution.  Even if it does apply, it's not clear
whether or not the expansion of ! to the history number should be
performed before or after the word expansions.  Bash chooses to do it
before the expansion, since that's when it's scanning the string for
the other backslash-prefixed expansions it performs, and it looks like
ksh does it after the word expansions.

Chet

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



Parent Message unknown Re: another problem with bash PS1 handling

by Nils-20 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 19 Okt., 02:56, Chet Ramey <chet.ra...@...> wrote:
> Second, there are a couple of problems with Posix and this construct.
> You can make an argument that Posix doesn't apply, since it only
> calls for parameter expansion on the value of PS1, and that does not
> include command substitution.  Even if it does apply, it's not clear
> whether or not the expansion of ! to the history number should be
> performed before or after the word expansions.  Bash chooses to do it
> before the expansion, since that's when it's scanning the string for
> the other backslash-prefixed expansions it performs, and it looks like
> ksh does it after the word expansions.

Whether POSIX applies or not, from a logical standpoint history number
and custom bash escape expansion should be last, after parameter
expansion and command substitution because doing it first breaks
command substitution (see my earlier post) or using variables in a
prompt (a='!'; PS1='$a ' will result in a literal "!" rather than the
history number), it is unintuitive and deviates from the behavior of
other shells such as variants of ash and ksh.

Re: another problem with bash PS1 handling

by Chet Ramey :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> Whether POSIX applies or not, from a logical standpoint history number
> and custom bash escape expansion should be last, after parameter
> expansion and command substitution because doing it first breaks
> command substitution (see my earlier post) or using variables in a
> prompt (a='!'; PS1='$a ' will result in a literal "!" rather than the
> history number), it is unintuitive and deviates from the behavior of
> other shells such as variants of ash and ksh.

I disagree.  I don't see a compelling reason to change 20 years of
behavior here.

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