this post was submitted on 17 Jan 2025
2 points (75.0% liked)

Bash

801 readers
3 users here now

Talk about the Bash Shell and Bash scripting

founded 4 years ago
MODERATORS
 

In the code below we wait for a background process twice. In the first paragraph we get the expected 0 return code from the wait. In the second paragraph the only change is wrapping the wait in a command substitution which instead gives a 255 return code. Why is this?

sleep 1 &
wait $!
echo $? # prints "0"

sleep 1 &
a=$(wait $!)  # <---- only difference is cmd substitution
echo $? # prints "255"

Thanks in advance for your thoughts!

top 1 comments
sorted by: hot top controversial new old
[–] OmnislashIsACloudApp 2 points 3 days ago

I would assume the cause is that the subshell does not have a previous command to wait on.