'Bash script not assigning the output of command to variable

So I have the following script:

#!/bin/bash
docker run -itd -p 8888:8888 --name notebook --mount type=bind,source="$(pwd)",target=/home/jovyan/work jupyter/datascience-notebook:latest
sleep 10s
link=$(docker logs --until=5s notebook | grep -o -m 1 'http://127.0.0.1:[0-9]*/lab?token=[0-9a-z]*')
var=$(date)

echo $var
echo $link

and when I run it with bash startup.sh it does not print out link variable while it does print var. The crazy thing is if I run these commands individually on my terminal it does set link (which is how I wrote the script in the first place). Also that 10 second sleep is more than enough for the docker logs to be available.

Am I missing something here? is there something special about docker that's causing this? I'm so confused.



Solution 1:[1]

So turns out the problem was that I wasn't waiting enough. After further debugging (shout out to Charles and Walter for their hints) I was able to fix it.

#!/bin/bash
docker run -itd -p 8888:8888 --name notebook --mount type=bind,source="$(pwd)",target=/home/jovyan/work jupyter/datascience-notebook:latest

while true; do
  link=$(docker logs --until=5s notebook | grep -o -m 1 'http://127.0.0.1:[0-9]*/lab?token=[0-9a-z]*')
  [ -z "$link" ] || break
  sleep 1
done

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wildhammer