# Unable to use variable in curl bash script

**URL:** <https://discuss.elastic.co/t/unable-to-use-variable-in-curl-bash-script/240003>\
**Category:** Elasticsearch\
**Created:** [July 6, 2020, 12:42pm UTC](https://discuss.elastic.co/t/unable-to-use-variable-in-curl-bash-script/240003 "2020-07-06T12:42:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jose\_H](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jose\_H](https://discuss.elastic.co/u/Jose_H)\
**Post date:** [July 6, 2020, 12:42pm UTC](https://discuss.elastic.co/t/unable-to-use-variable-in-curl-bash-script/240003/1 "2020-07-06T12:42:19Z")

</div>

Hi guys. I've been trying to use a variable called "Time Period Variable" in my bash script, but im unable to make it work:

> #!/bin/bash
> 
> # ES EVENT Threshold
> 
> number=10
> 
> # Time Period Variable
> 
> es\_period=now-1d
> 
> # Elasticsearch query - Value Count
> 
> es\_query=$(curl -s --cacert /etc/elasticsearch/certs/ca.crt -u elastic:PASSWORD -XGET "[https://elastic.local:9200/csco-fmc-ms/\_search](https://elastic.local:9200/csco-fmc-ms/_search)" -H 'Content-Type: application/json' -d'{ "from" : 0, "size" : 1000, "query": { "bool" : { "must" : { "wildcard" : { "@computed.message.keyword": "\*" } }, "filter": { "range": { "@timestamp": { "gte": "$es\_period", "lte": "now" } } } } }}')
> 
> # Execution
> 
> if [["$es\_query" -ge "$number"]]; then  
> echo "Threshold violated!";  
> else  
> echo "Threshold not violated.";  
> fi

I've tried to escape the quote as so

> "gte: \"$es\_period\"

but this is to no avail.

Any ideas how I can tackle this?

---

<div class="post-metadata">

**Author:** ![Yang\_Wang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yang_wang/32/48857_2.png) [@Yang\_Wang](https://discuss.elastic.co/u/Yang_Wang)\
**Post date:** [July 7, 2020, 6:39am UTC](https://discuss.elastic.co/t/unable-to-use-variable-in-curl-bash-script/240003/2 "2020-07-07T06:39:20Z")

</div>

Variable substitution does not work inside single quotes. You can try something like the follows:

> es\_query=$(curl -s --cacert /etc/elasticsearch/certs/ca.crt -u elastic:PASSWORD -XGET "[https://elastic.local:9200/csco-fmc-ms/\_search](https://elastic.local:9200/csco-fmc-ms/_search) 1" -H 'Content-Type: application/json' -d'{ "from" : 0, "size" : 1000, "query": { "bool" : { "must" : { "wildcard" : { "@computed.message.keyword": "\*" } }, "filter": { "range": { "@timestamp": { "gte": "'$es\_period'", "lte": "now" } } } } }}')

---

<div class="post-metadata">

**Author:** ![Jose\_H](https://avatars.discourse-cdn.com/v4/letter/j/e99b99/32.png) [@Jose\_H](https://discuss.elastic.co/u/Jose_H)\
**Post date:** [July 9, 2020, 11:03am UTC](https://discuss.elastic.co/t/unable-to-use-variable-in-curl-bash-script/240003/3 "2020-07-09T11:03:30Z")

</div>

Worked great, thank you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 6, 2020, 11:03am UTC](https://discuss.elastic.co/t/unable-to-use-variable-in-curl-bash-script/240003/4 "2020-08-06T11:03:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
