Double dollar sign not working correctly in script

Hi all,

I try to run commands on remote server via ssh and I need to use “if” condition under ssh command:

ssh $SSH_USER@$HOST "if [[ $(systemctl is-active --quiet my-service) ]]; then systemctl stop my-service; systemctl disable my-service; fi"

By default $(systemctl is-active --quiet my-service) will be expanded and I try to use double dollar sign to avoid that:

ssh $SSH_USER@$HOST "if [[ $$(systemctl is-active --quiet my-service) ]]; then systemctl stop my-service; systemctl disable my-service; fi"

but I got strange behavior, double dollar sign was replaced with “1”:

++ echo '$ ssh $SSH_USER@$HOST "if [[ $$(systemctl is-active --quiet my-service) ]]; then systemctl stop my-service; systemctl disable my-service; fi"'
++ ssh root@test.local 'if [[ 1(systemctl is-active --quiet my-service) ]]; then systemctl stop my-service; systemctl disable my-service; fi'

If I define $(systemctl is-active --quiet my-service) in variables block using double dollar sign and apply this variable in ssh it works fine:

...
  variables:
    SSH_CHECK_SERVICE: $$(systemctl is-active --quiet my-service)
  script:
    ssh $SSH_USER@$HOST "if [[ $SSH_CHECK_SERVICE ]]; then systemctl stop my-service; systemctl disable my-service; fi"
...

Is there any way to avoid using extra variable?

Many thanks,
Kirill

You can use backslash to escape dollar sign:

ssh $SSH_USER@$HOST "if [[ \$(systemctl is-active --quiet my-service) ]]; then systemctl stop my-service; systemctl disable my-service; fi"