Expansions inside "
work.
With set -x
it looks like this:
$ set -x
$ nohup sh -c " i=1; while [ $(( $CONTAINERS_COUNT )) -ge $(( i )) ]
> do
> i=$((i+1))
> date
> done &"
+ nohup sh -c ' i=1; while [ 0 -ge 0 ]
do
i=1
date
done &'
You loop is expanded to while [ 0 -ge 0 ]
before executing sh
. So all sh
sees is [ 0 -ge 0 ]
- an endless loop.
I suggest using single quotes for script content to handle most quoting problems and pass environment using exported variable or by script positional arguments:
nohup sh -c '
CONTAINERS_COUNT=$1
i=1;
while (( CONTAINERS_COUNT >= i )); do
i=$((i+1))
date
done
' -- "$CONTAINERS_COUNT" &
Side note: it's just seq "$CONTAINERS_COUNT" | xargs -I{} date