Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

"jobs" command within substitution



Hello, all. This is my first post to this group.

I scoured the manpages and list archives, but could not find the answer I seek.

My question, in a nutshell, is: How can I effectively use the "jobs" builtin command in producing the interactive prompt?

-- THE PROBLEM --

The following line was the obvious choice (provided appropriate options are set), and indeed works fine in several other shells:

: PS1='$(jobs | pjobs_gen_prompt)'
: # pjobs_gen_prompt is a custom shell fn

However, I've discovered that, the output of

: echo $(jobs)

is empty (apart from the spurious newline), even when executing "jobs" in the current shell would produce output. In fact,

: ( jobs )

still produces the expected, same results as execution directly within the current shell.

It might be argued, due to the language of POSIX ("The jobs utility shall display the status of jobs that were started in the current shell environment"), that it is not required to do so for /copies/ of the current shell environment: however, the case between $(jobs) and (jobs) should at least be consistent, since their behavior in POSIX is defined the same. I don't think the language in the standard is precise enough to warrant me claiming that this actually /breaks/ POSIX, but it seems against the likely /intentions/ at any rate, at least to me.

My personal opinion is that this is a bug, but I'd be happy to hear explanations if this is intended behavior.

-- WORKAROUNDS? --

Regardless of whether this is a bug or not, I still need a way to do what I want. You can get a very clear picture of what I'm trying to accomplish by downloading my script (it lacks spit and polish that I plan to add soon, so it's not the "official" version yet; but it's functional):

http://micah.cowan.name/svn/promptjobs/trunk/prompt-jobs.sh

^^^ source this, DON'T execute it (it won't work).

and sourcing it (". ./prompt-jobs.sh"). After this, run a couple of jobs in the foreground (say, "man man" or "ls | less") and then suspend them via Ctrl-Z. In bash, this will produce a prompt such as:

  micah(1:man 2:ls)$

The prompt will be colorized in color-supporting terminals.

I wish to get the same effect in zsh. You can follow the same steps in zsh, and will get the colorized version of your prompt, but you won't get the joblists.

In the case of bash, I'm actually not using command-substitution on the value of PS1, but I'm using bash's PROMPT_COMMAND to /set/ PS1's value via a command-substitution. This is because, when bash is interpolating the value from PS1, it treats the escape-sequence protectors \[ and \] (equivalent to zsh's %{, %}) before it does the command-substitution, which would cause pjobs_gen_prompt's \[, \] to be printed literally. Zsh treats %{ and %} after command-substitution, so that's not a problem.

Is there any /other/ way to do this that will work, today, for zsh? Perhaps an equivalent to bash's PROMPT_COMMAND that I haven't found in the documentation, or some option I missed that will allow jobs within command substitution to produce the output I'm expecting?

Thanks /very/ much for taking the time to read this length explanation. And, please let me know what you think of my little script :)

--
Micah J. Cowan
Programmer, musician, typesetting enthusiast, gamer...
http://micah.cowan.name/



Messages sorted by: Reverse Date, Date, Thread, Author