'Substituting New Relic query element with Bash variable - Bash

I would like to query New Relic (a logging/monitoring service) for some metrics using the New Relic API. I am running this query through a Docker container using a Bash script, and attempting to pass in a dynamic variable.

Here is my simplified script:

##!/bin/bash

day='Since 1 day ago'

#GPU Metrics Query
newrelic nrql query --accountId {account_id} --query 'SELECT * FROM NvidiaGpuMetricSample $day'

The query that the New Relic API accepts is:

newrelic nrql query --accountId {account_id} --query 'SELECT * FROM NvidiaGpuMetricSample Since 1 day ago'

And the error message I receive with this Bash script above is:

level=fatal msg="NRQL Syntax Error: Error at line 1 position 37, unexpected '$'"

I have tried to use different methods to pass in the variable, for example using 'eval $day' and passing that in a new variable or using /"$day"/ within the query string, but nothing seems to give the API what it is looking for.

Summary: I would like to pass a string variable into a string query using Bash.



Solution 1:[1]

Use double quotes instead of single quotes.

newrelic nrql query --accountId {account_id} --query "SELECT * FROM NvidiaGpuMetricSample $day"

Referencing variables in Bash is possible only inside double quotes, see https://tldp.org/LDP/abs/html/quotingvar.html

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 user14967413