<HTML ><HEAD ><TITLE >Stopped Jobs Count</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Bash Prompt HOWTO" HREF="index.html"><LINK REL="UP" TITLE="Prompt Code Snippets" HREF="c679.html"><LINK REL="PREVIOUS" TITLE="Checking the Current TTY" HREF="x721.html"><LINK REL="NEXT" TITLE="Load" HREF="x746.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Bash Prompt HOWTO: </TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x721.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 11. Prompt Code Snippets</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x746.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN733" ></A >11.6. Stopped Jobs Count</H1 ><P >Torben Fjerdingstad (<TT CLASS="EMAIL" ><<A HREF="mailto:tfj at fjerdingstad dot dk" >tfj at fjerdingstad dot dk</A >></TT >) wrote to tell me that he often stops jobs and then forgets about them. He uses his prompt to remind himself of stopped jobs. Apparently this is fairly popular, because as of Bash 2.04, there is a standard escape sequence for jobs managed by the shell: </P ><TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" >[giles@zinfandel]$ export PS1='\W[\j]\$ ' giles[0]$ man ls & [1] 31899 giles[1]$ xman & [2] 31907 [1]+ Stopped man ls giles[2]$ jobs [1]+ Stopped man ls [2]- Running xman & giles[2]$</PRE ></FONT ></TD ></TR ></TABLE ><P >Note that this shows both stopped and running jobs. At the console, you probably want the complete count, but in an xterm you're probably only interested in the ones that are stopped. To display only these, you could use something like the following:</P ><TABLE BORDER="1" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="SCREEN" >[giles@zinfandel]$ function stoppedjobs { -- jobs -s | wc -l | sed -e "s/ //g" -- } [giles@zinfandel]$ export PS1='\W[`stoppedjobs`]\$ ' giles[0]$ jobs giles[0]$ man ls & [1] 32212 [1]+ Stopped man ls giles[0]$ man X & [2] 32225 [2]+ Stopped man X giles[2]$ jobs [1]- Stopped man ls [2]+ Stopped man X giles[2]$ xman & [3] 32246 giles[2]$ sleep 300 & [4] 32255 giles[2]$ jobs [1]- Stopped man ls [2]+ Stopped man X [3] Running xman & [4] Running sleep 300 &</PRE ></FONT ></TD ></TR ></TABLE ><P >This doesn't always show the stopped job in the prompt that follows immediately after the command is executed - it probably depends on whether the job is launched and put in the background before <B CLASS="COMMAND" >jobs</B > is run.</P ><DIV CLASS="NOTE" ><P ></P ><TABLE CLASS="NOTE" WIDTH="100%" BORDER="0" ><TR ><TD WIDTH="25" ALIGN="CENTER" VALIGN="TOP" ><IMG SRC="../images/note.gif" HSPACE="5" ALT="Note"></TD ><TD ALIGN="LEFT" VALIGN="TOP" ><P >There is a known bug in Bash 2.02 that causes the <B CLASS="COMMAND" >jobs</B > command (a shell builtin) to return nothing to a pipe. If you try the above under Bash 2.02, you will always get a "0" back regardless of how many jobs you have stopped. This problem is fixed in 2.03. </P ></TD ></TR ></TABLE ></DIV ><P >Relative speed: 'jobs -s | wc -l | sed -e "s/ //g" ' takes about 0.24 seconds on an unloaded 486SX25. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x721.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x746.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Checking the Current TTY</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c679.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Load</TD ></TR ></TABLE ></DIV ></BODY ></HTML >