Jobs in bash

In most cases when I’m working with the shell, I send my applications to background, mostly my emacs. Nonetheless I forget quickly, and I open too many times the same file.

Due to my lack of memory I decided to make a function that shows me how many programs I have in background, here is the function:

function get_njobs {
 njobs=$(jobs | wc -l)

 if [ $njobs -gt "0" ];
 then
   echo $njobs |  sed -e 's/\([0-9]*\)/(\1)/g'
 fi
}

function prompt {

#  Shell into Emacs
 if [ $TERM != "dumb" ];
 then
  alias ls='ls --color=auto'
  PS1="${purpple}\u@${close}${YELLOW}\h${close}:{\W}\$(get_njobs)"
 else
    PS1="[\u@\h:\w]"
 fi
}            

Now you can do some work in background and will see what I mean:

user@host:{~} emacs &
user@host:{~}(1)

As you may notice, the number inside the brackets remembers me if there is some program running in background. Do not forget to add the function at the end of your bashrc.

UPDATE: If you add \j to your PS1 variable you will get the same effect but if there is not jobs you will always get a (0).

Comments