Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible to customise the bash prompt to show the if there are any background jobs? I find it easy to forget that there are background jobs.

Say if the prompt was...

$

Is there a way to make it show the number of background jobs? For example, if there were two background jobs sent to the background using CTRL+Z, the prompt would be...

2 $
share|improve this question

1 Answer 1

up vote 12 down vote accepted

Put \j in your prompt. From the bash manual:

\j The number of jobs currently managed by the shell


Just remember that prompts do go stale and jobs can finish at any time, so if you have left the terminal idle, you'll want to redisplay the prompt.


At the cost of requiring an extra process just to print your prompt, you can make the \j only appear if any jobs exist.

PROMPT_COMMAND='hasjobs=$(jobs -p)'
PS1='${hasjobs:+\j }\$ '
share|improve this answer
    
I want to make it so that it only shows this when there are background jobs, and have the usual prompt when there aren't any. What could I have it check for, so that I can use if and else (or is there a better way)? Thanks. –  paradroid Apr 4 '12 at 21:11
    
@paradroid Sorry, I don't know of any non-ugly way to do that. Are you really sure that is necessary? –  jw013 Apr 9 '12 at 23:54
    
Well, I'd like to do it, as long as it works fast enough. –  paradroid Apr 10 '12 at 0:50
    
@paradroid I edited the answer to add a semi-ugly hack. As far as speed, the performance hit will be negligible on most modern systems. –  jw013 Apr 10 '12 at 1:24
1  
Sorry, not a clairvoyant -- you'll have to be more specific about your unexpected results and what you did to get them. –  jw013 Apr 10 '12 at 2:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.