help-bash
[Top][All Lists]
Advanced

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

Re: jobs built-in command and PROMPT_COMMAND


From: Christoph Anton Mitterer
Subject: Re: jobs built-in command and PROMPT_COMMAND
Date: Sun, 03 Oct 2021 01:20:49 +0200
User-agent: Evolution 3.42.0-2

On Fri, 2021-10-01 at 09:22 -0400, Greg Wooledge wrote:
> > PS1='...$( [ -n "$(jobs)"] && printf '\j')...'
> 
> So many forks. :(

Uhm... one for each command substitution


> You can sidestep the prompt forks in most cases by using the value as
> an array index, with a specially crafted array.  Here's a brief
> example:
> 
> unicorn:~$ a=(zero one two three four)
> unicorn:~$ PS1='${a[\j]} \h:\w\$ '
> zero unicorn:~$ sleep 3 &
> [1] 527769
> one unicorn:~$ 
> [1]+  Done                    sleep 3
> zero unicorn:~$ 
> zero unicorn:~$ 
> 
> You can make element zero the empty string, make element one "1 ", and
> so on.  Then use ${a[\j]} with no space after it.  Or get more
> creative.

But doesn't all that require an array with at least as many elements
pre-set, as jobs my run?

 heisenberg:~$ sleep 100 &
[1] 80399
1 heisenberg:~$ sleep 100 &
[2] 80407
2 heisenberg:~$ sleep 100 &
[3] 80416
3 heisenberg:~$ sleep 100 &
[4] 80421
4 heisenberg:~$ sleep 100 &
[5] 80422
 heisenberg:~$ sleep 100 &
[6] 80424
 heisenberg:~$ 

I mean I don't like my forks either, and actually it causes some
noticeable effects already, when I keep pressing ENTER, I already start
to see empty lines between the prompts every now and then... but the
above seems also a bit hacky.


> Another common trick used in this situation is double-negation with
> the
> arithmetic ! operator.  In a math context (such an array indexing),
> !!foo
> evaluates to 0 if $foo is 0, or 1 if $foo is nonzero.  Then you can
> use an
> array with only two elements, to get two different strings based on
> whether $foo is zero or nonzero.  For example:
> 
> unicorn:~$ a=(none some)
> unicorn:~$ PS1='${a[!!\j]} \h:\w\$ '
> none unicorn:~$ sleep 3 &
> [1] 528973
> some unicorn:~$ sleep 4 &
> [2] 528980
> [1]   Done                    sleep 3
> some unicorn:~$ 
> some unicorn:~$ 
> [2]+  Done                    sleep 4
> none unicorn:~$ 
> 
> There are lots of things you can do with this trick.  Also, remember
> that
> you can assign extra variables in the PROMPT_COMMAND, and then use
> those
> variables in PS1.  All without forking.

That's quite nice actually... though it would only give me whether or
not jobs are running/stopped. Still pretty nice though.


> I think you're looking for this:
> 
>               checkjobs

That's anyway already the case for stopped jobs, isn't it?

But I'm afraid that's not enough in practise O:-)
Seems it happens every now and then that it use Ctrl-D more than once
and well... yes... it's my fault, I know,... but in my defence:
if you look at the number of stackoverflow questions with respect to
this matter, I'm not alone ;-)



Anyway guess the original questions of that thread are rather
unsolvable.


Thanks,
Chris.



reply via email to

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