Understanding the Behavior Differences between jobs Command zsh and bash

Asked 2 years ago, Updated 2 years ago, 112 views

I'm having trouble with the behavior difference in zsh when using the jobs command. Specifically,

#!/bin/zsh
for in {1..10}; do
    sleep1&
    jobs | wc-l
    jobs
done

If you execute this, the number of lines counted by wc will remain at 0.
while the command (sleep) being executed increases. Try #!/bin/bash instead of #!/bin/zsh and it will work as expected (wc-l output is increasing).

Also, even in the case of zsh, if the above program is entered directly on the terminal, we have confirmed that it works as expected like bash.

Run in
·OS: Debian (stretch 9.3)
·zsh: 5.3.1
·bash: 4.4.12
That's right.

I'm sorry to trouble you, but I'd appreciate it if you could let me know if anyone knows the cause.
I look forward to your kind cooperation.

bash zsh

2022-09-29 21:32

1 Answers

Although you have not read the details carefully, running setopt monitor results in the desired behavior.Note: https://unix.stackexchange.com/a/227411/157713

#!/bin/zsh

setopt monitor

for in {1..10}; do
    sleep1&
    jobs | wc-l
    jobs
done
# =>wc results show the number of jobs


2022-09-29 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.