Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16802

Re: How to use PROMPT_COMMAND(S) without breaking other scripts

Path csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail
From Martijn Dekker <martijn@inlv.org>
Newsgroups gnu.bash.bug
Subject Re: How to use PROMPT_COMMAND(S) without breaking other scripts
Date Mon, 24 Aug 2020 17:58:27 +0100
Lines 54
Approved bug-bash@gnu.org
Message-ID <mailman.1025.1598288325.2469.bug-bash@gnu.org> (permalink)
References <CAKOZuevPZ1xwhLmLzXt_a=G+azDTouO52MCA=wvh=-M82wzaSQ@mail.gmail.com> <17a0ba52-32ee-b9bc-72ff-3587b2050fbd@case.edu> <CAFLRLk9vNPM5NtypB2UNcDqMZfG3Lmi3zEMGfPqZNacmHCoAoA@mail.gmail.com> <0c10b76f-a7f0-1c52-77b7-4b208035956b@case.edu> <c904f5b5-1312-ac4c-26bd-bfb97c1f43e8@inlv.org>
NNTP-Posting-Host lists.gnu.org
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 8bit
X-Trace usenet.stanford.edu 1598288325 6831 209.51.188.17 (24 Aug 2020 16:58:45 GMT)
X-Complaints-To action@cs.stanford.edu
To bug-bash@gnu.org
Envelope-to bug-bash@gnu.org
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:68.0) Gecko/20100101 Thunderbird/68.11.0
In-Reply-To <0c10b76f-a7f0-1c52-77b7-4b208035956b@case.edu>
Content-Language en-GB
Received-SPF none client-ip=2a02:2770::21a:4aff:fec6:e3d8; envelope-from=martijn@inlv.org; helo=freekahlil.inlv.org
X-detected-operating-system by eggs.gnu.org: No matching host in p0f cache. That's all we know.
X-Spam_score_int -40
X-Spam_score -4.1
X-Spam_bar ----
X-Spam_report (-4.1 / 5.0 requ) BAYES_00=-1.9, NICE_REPLY_A=-2.25, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no
X-Spam_action no action
X-BeenThere bug-bash@gnu.org
X-Mailman-Version 2.1.23
Precedence list
List-Id Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org>
List-Unsubscribe <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe>
List-Archive <https://lists.gnu.org/archive/html/bug-bash>
List-Post <mailto:bug-bash@gnu.org>
List-Help <mailto:bug-bash-request@gnu.org?subject=help>
List-Subscribe <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe>
X-Mailman-Original-Message-ID <c904f5b5-1312-ac4c-26bd-bfb97c1f43e8@inlv.org>
X-Mailman-Original-References <CAKOZuevPZ1xwhLmLzXt_a=G+azDTouO52MCA=wvh=-M82wzaSQ@mail.gmail.com> <17a0ba52-32ee-b9bc-72ff-3587b2050fbd@case.edu> <CAFLRLk9vNPM5NtypB2UNcDqMZfG3Lmi3zEMGfPqZNacmHCoAoA@mail.gmail.com> <0c10b76f-a7f0-1c52-77b7-4b208035956b@case.edu>
Xref csiph.com gnu.bash.bug:16802

Show key headers only | View raw


Op 24-08-20 om 15:57 schreef Chet Ramey:
> I sometimes think I should have stuck with converting PROMPT_COMMAND to
> an array. Either way, there's going to be a transition, and maybe that
> would have been the easiest.

Is it too late? I think that would actually be cleaner than adding a 
separate array, per Koichi's report.

One problem is that if a script does the obvious

     PROMPT_COMMAND+=("some command here")

if PROMPT_COMMAND is not yet set, then it starts adding at array index 
0, so a subsequent traditional usage from some other script

     PROMPT_COMMAND="some command here"

would overwrite it. So array usage should not use the 0 index. To avoid 
using the 0 index, one possibility is:

     PROMPT_COMMAND[$(( ${#PROMPT_COMMAND[@]} + 1 ))]="some command here"

which, if PROMPT_COMMAND is unset, starts adding at index 1, not 0, and 
otherwise acts identically. However, 'set -u'/'set -o nounset' kills 
that. That option makes the ${#PROMPT_COMMAND[@]} expansion error out if 
there are no array elements (even though ${#@} works with no positional 
parameters). It's also an unwieldy command. So maybe that idea is not 
the best.

Another way to avoid using the 0 index, which is 'set -u' compatible, 
would be

     PROMPT_COMMAND=${PROMPT_COMMAND-}
     PROMPT_COMMAND+=("some command here")

The first command sets PROMPT_COMMAND[0] to the empty value if it 
doesn't exist yet, and otherwise leaves it unchanged. It's a bit of an 
ugly hack though.

But then, maybe it's best if bash itself just sets PROMPT_COMMAND[0] to 
the empty value on initialisation. IMO that would be a reasonably clean 
and reliable way to ensure a smooth transition.

Just my 2ยข,

- M.

-- 
||	modernish -- harness the shell
||	https://github.com/modernish/modernish
||
||	KornShell lives!
||	https://github.com/ksh93/ksh

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: How to use PROMPT_COMMAND(S) without breaking other scripts Martijn Dekker <martijn@inlv.org> - 2020-08-24 17:58 +0100

csiph-web