Re: [Help-bash] Expand a prompt string to calculate its length?
From:
Andy Chu
Subject:
Re: [Help-bash] Expand a prompt string to calculate its length?
Date:
Wed, 19 Jun 2019 11:18:29 -0700
The @P techniques works if you don't have color in your prompt. If you
have colors I'm not sure there's a way to do it in general.
The \[ and \] escape codes in PS1 are used to compensate for color when
passing to GNU readline. They get translated to \0x00 and \0x01 if I
remember correctly. GNU readline also has to have a notion of the prompt
width.
Andy
On Wed, Jun 19, 2019 at 11:14 AM Greg Wooledge <address@hidden> wrote:
> On Wed, Jun 19, 2019 at 01:09:33PM -0500, Jesse Hathaway wrote:
> > Is there anyway to expand a partial prompt string to calculate its
> length?
>
> In bash 4.4 and above, yes.
>
> wooledg:~$ tmp=${PS1@P}; echo "<$PS1> <${#PS1}> <$tmp> <${#tmp}>"
> <\h:\w\$ > <8> <wooledg:~$ > <11>
>
> The @P parameter expansion is one of the new "Parameter transformation"
> options.
>
>