1

The following is my linux shell code for insert one record to ElasticSearch:

username="Tom"
curl -XPOST 'http://192.168.0.1:9200/userdb/info/1' -d '{"user":$username}'

But it didn't work, it treated $username is a string not a variable. How can I fix this please?

1 Answer 1

3

You need to enclose $username in double quotes since it's a string. That will work.

username="Tom"
curl -XPOST 'http://192.168.0.1:9200/userdb/info/1' -d "{\"user\":\"$username\"}"
                                                       ^           ^         ^  ^
                                                       |           |         |  |
                                                     here         here  and  here
Sign up to request clarification or add additional context in comments.

5 Comments

It doesn't work, when I search it ,the result is :"_source":{"user":"$username"}, it didn't treat $username as a variable.
Strange, it works perfectly for me when in try in my shell. What kind of shell do you have?
I'm using #!/bin/bash
Oh you need double quotes all the way, otherwise the variable doesn't get expanded. See my answer and try again.
thank you so much, it works now. You are Linux and ES master :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.