help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Expand a prompt string to calculate its length?


From: Greg Wooledge
Subject: Re: [Help-bash] Expand a prompt string to calculate its length?
Date: Mon, 1 Jul 2019 08:55:02 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

(Douglas forgot to Cc the list)

On Sat, Jun 29, 2019 at 12:09:22PM -0400, Douglas Lewan wrote:
> On 06/19/2019 02:13 PM, Greg Wooledge 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.
> > 
> How do you know about the @P parameter extension? I don't find it in the
> info. I'm using bash 4.4.12.
> Just curious.
> (And what other bashisms aren't in the info?)

It's in the man page.  As for "how do you know about it", I'm pretty sure
I first learned about it by reading the 4.4 changelog.  I realize that
most bash users will not be in the habit of reading changelogs for each
new release, but that's why we have wikis and mailing lists and so on.

Here's the relevant section from the 4.4 man page as seen on Debian stable.
The 5.0 man page is very similar.


       ${parameter@operator}
              Parameter transformation.  The expansion is either a transforma‐
              tion of the value of parameter or  information  about  parameter
              itself,  depending on the value of operator.  Each operator is a
              single letter:

              Q      The expansion is a string that is the value of  parameter
                     quoted in a format that can be reused as input.
              E      The  expansion is a string that is the value of parameter
                     with backslash escape  sequences  expanded  as  with  the
                     $'...' quoting mechansim.
              P      The expansion is a string that is the result of expanding
                     the value of parameter as if it were a prompt string (see
                     PROMPTING below).
              A      The  expansion  is  a string in the form of an assignment
                     statement or declare command  that,  if  evaluated,  will
                     recreate parameter with its attributes and value.
              a      The  expansion is a string consisting of flag values rep‐
                     resenting parameter's attributes.

              If parameter is @ or *, the operation is applied to  each  posi‐
              tional  parameter  in  turn,  and the expansion is the resultant
              list.  If parameter is an array variable subscripted with  @  or
              *,  the case modification operation is applied to each member of
              the array in turn, and the expansion is the resultant list.

              The result of the expansion is subject  to  word  splitting  and
              pathname expansion as described below.


As usual with bash's man page, the actual syntax itself is NOT searchable.
E.g. you cannot search for @P because those two characters do not actually
appear together.  The Special Parameters section has this same issue
(e.g. you cannot search for $? or $!).  I actually submitted a patch to
try to change that, once, but the patch was rejected.

In order to find it in the man page, you have to know its name, which
is why I took great care to write the words "Parameter transformation",
with quotes around them to indicate specialness, and with the same
capitalization seen in the man page, for those whose pagers use
case-sensitive searches by default.



reply via email to

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