0

When I run this command, it literally posts $line into Elasticsearch instead of the lines of file.txt

cat file.txt | while IFS= read -r line; do curl -d '{ "field" : "$line" }' -H "Content-Type: application/json" -POST "http://localhost:9200/indexname/doc"; done;

file.txt consist of:

This is line one.
This is line two.
This is line three.

This is Elasticsearch 7.2 on Centos 7.

I tried escaping $line as in \$line and ($line) or "$line"
but it POSTS the actual value of \$line and ($line) or "$line"

Expected data to be put into Elasticsearch:

{ "field" : "This is line one." }
{ "field" : "This is line two." }
{ "field" : "This is line three." }

actual data put into Elasticsearch
{ "field" : "$line" }

  • 2
    See: [Difference between single and double quotes in bash](http://stackoverflow.com/q/6697753/3776858) – Cyrus Jul 09 '19 at 17:44

1 Answers1

0

I found it similar to this question: How can I use a variable in curl call within bash script and answer. Thank you @that other guy and @chepner

cat file.txt | while IFS= read -r line; do jq -n --arg var "$line"  '.field = $var'| curl --data @- -H "Content-Type: application/json" -POST "http://localhost:9200/indexname/doc"; done;