A typical prompt in bash in something like:
PS1="\u@\h:\w\$ "
The you can show the number of background jobs using \j
, e.g.:
PS1="\u@\h:\w [\j]\$ "
Which is useful, because every now and then I forget I have a stopped job and only notice when it complains if I manually logout from the shell.
However, 95% of the time, the background job count is 0 and showing it in the prompt is superfluous.
How can I show the job count in the prompt, but only if it's nonzero?
You can e.g. do something like this:
PS1='\u@\h:\w $([ \j -gt 0 ] && echo [\j])\$ '
The accepted answer does not work for me (I have got Bash v4.2.46). It throws an error like this:
[: \j: integer expression expected
I had to use PROMPT_COMMAND to achieve the same functionality:
export PROMPT_COMMAND=__prompt_command
function __prompt_command() {
local JOBS=$(jobs | wc -l | tr -d 0)
PS1="\u@\h:\w [${JOBS}]\$ "
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With