How to get variables expanded in output of jobs command












1















As you can see in the following output the variable $i in the output of ps aux is expanded to sleep 1, sleep 2, etc.
Is there a way to make zsh do the same? In the jobs output all the commands get the same name, which is sleep $i.



$ for i in {1..10}; do sleep $i& done; ps aux | grep sleep; jobs
[6] 1630
[7] 1631
[8] 1632
[9] 1633
[10] 1634
[11] 1635
[12] 1636
[13] 1637
[14] 1638
[15] 1639
root 1630 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 1
root 1631 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 2
root 1632 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 3
root 1633 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 4
root 1634 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 5
root 1635 0.0 0.0 5224 752 pts/3 SN 10:06 0:00 sleep 6
root 1636 0.0 0.0 5224 680 pts/3 SN 10:06 0:00 sleep 7
root 1637 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 8
root 1638 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 9
root 1639 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 10
root 1641 0.0 0.0 6144 880 pts/3 S+ 10:06 0:00 grep --color=auto sleep
[6] running sleep $i
[7] running sleep $i
[8] running sleep $i
[9] running sleep $i
[10] running sleep $i
[11] running sleep $i
[12] running sleep $i
[13] running sleep $i
[14] - running sleep $i
[15] + running sleep $i


Cheers!










share|improve this question



























    1















    As you can see in the following output the variable $i in the output of ps aux is expanded to sleep 1, sleep 2, etc.
    Is there a way to make zsh do the same? In the jobs output all the commands get the same name, which is sleep $i.



    $ for i in {1..10}; do sleep $i& done; ps aux | grep sleep; jobs
    [6] 1630
    [7] 1631
    [8] 1632
    [9] 1633
    [10] 1634
    [11] 1635
    [12] 1636
    [13] 1637
    [14] 1638
    [15] 1639
    root 1630 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 1
    root 1631 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 2
    root 1632 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 3
    root 1633 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 4
    root 1634 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 5
    root 1635 0.0 0.0 5224 752 pts/3 SN 10:06 0:00 sleep 6
    root 1636 0.0 0.0 5224 680 pts/3 SN 10:06 0:00 sleep 7
    root 1637 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 8
    root 1638 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 9
    root 1639 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 10
    root 1641 0.0 0.0 6144 880 pts/3 S+ 10:06 0:00 grep --color=auto sleep
    [6] running sleep $i
    [7] running sleep $i
    [8] running sleep $i
    [9] running sleep $i
    [10] running sleep $i
    [11] running sleep $i
    [12] running sleep $i
    [13] running sleep $i
    [14] - running sleep $i
    [15] + running sleep $i


    Cheers!










    share|improve this question

























      1












      1








      1








      As you can see in the following output the variable $i in the output of ps aux is expanded to sleep 1, sleep 2, etc.
      Is there a way to make zsh do the same? In the jobs output all the commands get the same name, which is sleep $i.



      $ for i in {1..10}; do sleep $i& done; ps aux | grep sleep; jobs
      [6] 1630
      [7] 1631
      [8] 1632
      [9] 1633
      [10] 1634
      [11] 1635
      [12] 1636
      [13] 1637
      [14] 1638
      [15] 1639
      root 1630 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 1
      root 1631 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 2
      root 1632 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 3
      root 1633 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 4
      root 1634 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 5
      root 1635 0.0 0.0 5224 752 pts/3 SN 10:06 0:00 sleep 6
      root 1636 0.0 0.0 5224 680 pts/3 SN 10:06 0:00 sleep 7
      root 1637 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 8
      root 1638 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 9
      root 1639 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 10
      root 1641 0.0 0.0 6144 880 pts/3 S+ 10:06 0:00 grep --color=auto sleep
      [6] running sleep $i
      [7] running sleep $i
      [8] running sleep $i
      [9] running sleep $i
      [10] running sleep $i
      [11] running sleep $i
      [12] running sleep $i
      [13] running sleep $i
      [14] - running sleep $i
      [15] + running sleep $i


      Cheers!










      share|improve this question














      As you can see in the following output the variable $i in the output of ps aux is expanded to sleep 1, sleep 2, etc.
      Is there a way to make zsh do the same? In the jobs output all the commands get the same name, which is sleep $i.



      $ for i in {1..10}; do sleep $i& done; ps aux | grep sleep; jobs
      [6] 1630
      [7] 1631
      [8] 1632
      [9] 1633
      [10] 1634
      [11] 1635
      [12] 1636
      [13] 1637
      [14] 1638
      [15] 1639
      root 1630 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 1
      root 1631 0.0 0.0 5224 684 pts/3 SN 10:06 0:00 sleep 2
      root 1632 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 3
      root 1633 0.0 0.0 5224 744 pts/3 SN 10:06 0:00 sleep 4
      root 1634 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 5
      root 1635 0.0 0.0 5224 752 pts/3 SN 10:06 0:00 sleep 6
      root 1636 0.0 0.0 5224 680 pts/3 SN 10:06 0:00 sleep 7
      root 1637 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 8
      root 1638 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 9
      root 1639 0.0 0.0 5224 748 pts/3 SN 10:06 0:00 sleep 10
      root 1641 0.0 0.0 6144 880 pts/3 S+ 10:06 0:00 grep --color=auto sleep
      [6] running sleep $i
      [7] running sleep $i
      [8] running sleep $i
      [9] running sleep $i
      [10] running sleep $i
      [11] running sleep $i
      [12] running sleep $i
      [13] running sleep $i
      [14] - running sleep $i
      [15] + running sleep $i


      Cheers!







      zsh background-process jobs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 14 at 9:11









      H. RosendahlH. Rosendahl

      587




      587






















          1 Answer
          1






          active

          oldest

          votes


















          2














          Note that it's not processes you put in foreground, but jobs, made of a shell command which could be a compound command starting several processes in parallel (like in sleep 10 | sleep 20 &) or one after the other (like in for i in {1..10}; do sleep $i; done &).



          And each of these process could in turn start more processes (which would still be part of the job, but unknow to zsh as not direct descendants) or could change their argument list as reported by ps (like in sh -c 'exec env sleep 10' which runs a process that executes sh, then env, then sleep all in the same process) or could leave the job (by becoming a new process group leader).



          It sounds like that for each job, you want to see the arg list of the processes in that job.



          Maybe something like:



          for job state ("${(@kv)jobstates}") {
          pgid=${${state%%=*}##*:}
          echo Job $job:
          pgrep -ag $pgid
          }


          Which on your example gives something like:



          Job 2:
          26590 sleep 1
          Job 3:
          26591 sleep 2
          Job 4:
          26592 sleep 3
          Job 5:
          26593 sleep 4
          Job 6:
          26594 sleep 5
          Job 7:
          26595 sleep 6
          Job 8:
          26596 sleep 7
          Job 9:
          26597 sleep 8
          Job 10:
          26598 sleep 9
          Job 11:
          26599 sleep 10





          share|improve this answer
























          • it's quite a luck that sleep isn't a builtin in zsh as in ksh.

            – pizdelect
            Feb 18 at 17:59











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500569%2fhow-to-get-variables-expanded-in-output-of-jobs-command%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Note that it's not processes you put in foreground, but jobs, made of a shell command which could be a compound command starting several processes in parallel (like in sleep 10 | sleep 20 &) or one after the other (like in for i in {1..10}; do sleep $i; done &).



          And each of these process could in turn start more processes (which would still be part of the job, but unknow to zsh as not direct descendants) or could change their argument list as reported by ps (like in sh -c 'exec env sleep 10' which runs a process that executes sh, then env, then sleep all in the same process) or could leave the job (by becoming a new process group leader).



          It sounds like that for each job, you want to see the arg list of the processes in that job.



          Maybe something like:



          for job state ("${(@kv)jobstates}") {
          pgid=${${state%%=*}##*:}
          echo Job $job:
          pgrep -ag $pgid
          }


          Which on your example gives something like:



          Job 2:
          26590 sleep 1
          Job 3:
          26591 sleep 2
          Job 4:
          26592 sleep 3
          Job 5:
          26593 sleep 4
          Job 6:
          26594 sleep 5
          Job 7:
          26595 sleep 6
          Job 8:
          26596 sleep 7
          Job 9:
          26597 sleep 8
          Job 10:
          26598 sleep 9
          Job 11:
          26599 sleep 10





          share|improve this answer
























          • it's quite a luck that sleep isn't a builtin in zsh as in ksh.

            – pizdelect
            Feb 18 at 17:59
















          2














          Note that it's not processes you put in foreground, but jobs, made of a shell command which could be a compound command starting several processes in parallel (like in sleep 10 | sleep 20 &) or one after the other (like in for i in {1..10}; do sleep $i; done &).



          And each of these process could in turn start more processes (which would still be part of the job, but unknow to zsh as not direct descendants) or could change their argument list as reported by ps (like in sh -c 'exec env sleep 10' which runs a process that executes sh, then env, then sleep all in the same process) or could leave the job (by becoming a new process group leader).



          It sounds like that for each job, you want to see the arg list of the processes in that job.



          Maybe something like:



          for job state ("${(@kv)jobstates}") {
          pgid=${${state%%=*}##*:}
          echo Job $job:
          pgrep -ag $pgid
          }


          Which on your example gives something like:



          Job 2:
          26590 sleep 1
          Job 3:
          26591 sleep 2
          Job 4:
          26592 sleep 3
          Job 5:
          26593 sleep 4
          Job 6:
          26594 sleep 5
          Job 7:
          26595 sleep 6
          Job 8:
          26596 sleep 7
          Job 9:
          26597 sleep 8
          Job 10:
          26598 sleep 9
          Job 11:
          26599 sleep 10





          share|improve this answer
























          • it's quite a luck that sleep isn't a builtin in zsh as in ksh.

            – pizdelect
            Feb 18 at 17:59














          2












          2








          2







          Note that it's not processes you put in foreground, but jobs, made of a shell command which could be a compound command starting several processes in parallel (like in sleep 10 | sleep 20 &) or one after the other (like in for i in {1..10}; do sleep $i; done &).



          And each of these process could in turn start more processes (which would still be part of the job, but unknow to zsh as not direct descendants) or could change their argument list as reported by ps (like in sh -c 'exec env sleep 10' which runs a process that executes sh, then env, then sleep all in the same process) or could leave the job (by becoming a new process group leader).



          It sounds like that for each job, you want to see the arg list of the processes in that job.



          Maybe something like:



          for job state ("${(@kv)jobstates}") {
          pgid=${${state%%=*}##*:}
          echo Job $job:
          pgrep -ag $pgid
          }


          Which on your example gives something like:



          Job 2:
          26590 sleep 1
          Job 3:
          26591 sleep 2
          Job 4:
          26592 sleep 3
          Job 5:
          26593 sleep 4
          Job 6:
          26594 sleep 5
          Job 7:
          26595 sleep 6
          Job 8:
          26596 sleep 7
          Job 9:
          26597 sleep 8
          Job 10:
          26598 sleep 9
          Job 11:
          26599 sleep 10





          share|improve this answer













          Note that it's not processes you put in foreground, but jobs, made of a shell command which could be a compound command starting several processes in parallel (like in sleep 10 | sleep 20 &) or one after the other (like in for i in {1..10}; do sleep $i; done &).



          And each of these process could in turn start more processes (which would still be part of the job, but unknow to zsh as not direct descendants) or could change their argument list as reported by ps (like in sh -c 'exec env sleep 10' which runs a process that executes sh, then env, then sleep all in the same process) or could leave the job (by becoming a new process group leader).



          It sounds like that for each job, you want to see the arg list of the processes in that job.



          Maybe something like:



          for job state ("${(@kv)jobstates}") {
          pgid=${${state%%=*}##*:}
          echo Job $job:
          pgrep -ag $pgid
          }


          Which on your example gives something like:



          Job 2:
          26590 sleep 1
          Job 3:
          26591 sleep 2
          Job 4:
          26592 sleep 3
          Job 5:
          26593 sleep 4
          Job 6:
          26594 sleep 5
          Job 7:
          26595 sleep 6
          Job 8:
          26596 sleep 7
          Job 9:
          26597 sleep 8
          Job 10:
          26598 sleep 9
          Job 11:
          26599 sleep 10






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 18 at 17:39









          Stéphane ChazelasStéphane Chazelas

          308k57581939




          308k57581939













          • it's quite a luck that sleep isn't a builtin in zsh as in ksh.

            – pizdelect
            Feb 18 at 17:59



















          • it's quite a luck that sleep isn't a builtin in zsh as in ksh.

            – pizdelect
            Feb 18 at 17:59

















          it's quite a luck that sleep isn't a builtin in zsh as in ksh.

          – pizdelect
          Feb 18 at 17:59





          it's quite a luck that sleep isn't a builtin in zsh as in ksh.

          – pizdelect
          Feb 18 at 17:59


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f500569%2fhow-to-get-variables-expanded-in-output-of-jobs-command%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          第一次世界大戦

          Touch on Surface Book