Search code examples
google-cloud-platformshgoogle-cloud-spanner

How to use a sh variable in a gcp sql query command


I've a sh script which runs a sql query in a gcp > spanner db.

The query works fine. I need to incorporate in the sql-query code the value of one of the variable ID which is declared in the sh script.

What would be the right syntax for this case?

export DATABASE='my_db'
export ID='my_id'


    gcloud spanner databases execute-sql $DATABASE  \
        --instance=stable-id  \
        --sql='SELECT * FROM $ID' \
        --format=json > spanner-$ID.json

this variable $ID works fine in the script, but doesn't in the line where i call the query

        --sql='SELECT * FROM $ID' \

Solution

  • You need to use double-quotes instead of single-quotes for this to work.

    export DATABASE='my_db'
    export ID='my_id'
    
    
        gcloud spanner databases execute-sql $DATABASE  \
            --instance=stable-id  \
            --sql="SELECT * FROM $ID" \
            --format=json > spanner-$ID.json
    

    Should work.