bash: show names of background jobs in the bash prompt

0

It would be cool if the bash prompt can be used a a mini task bar.

Can the bash prompt (PS1) show the names of the jobs listed in the jobs command?

american-ninja-warrior

Posted 2016-01-27T00:33:25.990

Reputation: 211

You can put anything you like in PS1, including $(jobs|Filter), where Filter massages the output into whatever format you want. To get everything on one line use echo $($(jobs|Filter)). You can of course add all the other characters you would normally use in PS1, but you must use single quotes when assigning it, to make sure that the embedded command is expanded when the prompt is issued, rather than when the variable is set. – AFH – 2016-01-27T02:39:38.903

Answers

0

AFH in the comment gives you the builing blocks. Here is one example how to get the quoting correct.

as "name of the job", I choose the command name of the job (the first word after "Running"):

PS1='$(echo $(jobs | awk '\''/Running/{print "[" $3 "]"}'\''))$ '

And when trying that out with two background commands, and just typing "enter" a few times to see the prompt changing:

$ sleep 4 & sleep 2 &
[1] 7222
[2] 7223
[sleep] [sleep]$ 
[sleep] [sleep]$ 
[2]+  Done                    sleep 2
[sleep]$ 
[1]+  Done                    sleep 4
$ 

PBI

Posted 2016-01-27T00:33:25.990

Reputation: 281