Writing a Bash Script to Get the Token of a Jupyter Notebook Running in a Container

I run a Dockerized Jupyter Server and I don’t want to retrieve its access token manually. So I’d like to write a bash script which will extract the token and open the server with it

I tried as follows:

docker run --name notebook -w /home/fenics -v $(pwd):/home/fenics/shared -d -p quay.io/fenicsproject/stable 'jupyter-notebook --ip='
token=`docker logs notebook 2>&1 | grep -o "token=[a-z0-9]*" | sed -n 1p`

However, it doesn’t work. Since the command docker logs notebook doesn’t output anything if written in a script or in console on the same line with the command prior to it. But it does, if you write the commands in terminal one by one.
Please, show me how the problem can be cast (it would be OK even if your solution is different) and explain why my code can’t work (I do want to know because it is already the second problem I am trying to solve in this way)

Source: StackOverflow