Bash – Is it possible to customise the prompt to show the if there are any background jobs

background-processbashjobsprompt

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 $

Best Answer

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 }\$ '
Related Question