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 $

2 Answers 2


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 }\$ '
  • 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, 2012 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, 2012 at 23:54
  • Well, I'd like to do it, as long as it works fast enough.
    – paradroid
    Apr 10, 2012 at 0:50
  • 1
    @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, 2012 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, 2012 at 2:12

In zsh, you can use %j to indicate the number of jobs and "%1(j.%j.)" to show the number of jobs if there is at least one running and otherwise show nothing (instead of 0). Eg. RPROMPT="%1(j.%j.)" to show it in the prompt on the right hand side. More info in the manual and this SO answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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