2

This is what I tested in Arch's dash shell and FreeBSD's default sh shell, the results are the same.

$ sleep 100 &
$ jobs
[1] + Running                    sleep 100
$ jobs | wc
      0       0       0
$ jobs | grep 1

I can't wc or grep on the output of jobs command. The jobs command is shell built-in.

What is the reason for this? is it that jobs somehow prints different information in interactive shell and to pipe? I don't know details about that, but I knew about similar behavior of ls. I.e., you will see compact output of ls in shell, but grep will treat each file name in a standalone line instead.

Edit: I found a similar question: How to kill all background jobs in dash?, that question is also due to not being able to get the output of jobs command.

Lu Xu
  • 121
  • 4
  • How about redirecting output of jobs to temp file and reading from it? – SHW Oct 08 '21 at 10:58
  • @SHW do you know why this works? Where does jobs actually write? – Panki Oct 08 '21 at 11:00
  • @Panki, Not really. I just wanted a quick way to sort. But my first guess will be STDERR to STDOUT – SHW Oct 08 '21 at 11:07
  • @SHW redirecting to files indeed works, also it was suggested by someone else in the comment of the question I linked here. As for the stderr to stdout, if I am doing right, by jobs 2>&1 | wc, then it's not working either. Actually, if the jobs output was to stderr, it should be shown together with wc/grep output, instead of disappear. – Lu Xu Oct 08 '21 at 12:08

0 Answers0