Shell script -- how to store curl command WITH variables result into another variable?

JChao

Currently I have a curl command that I want to store in a local variable. I know I could use `` or $() to get it done, but a variable inside the command stops me from doing so.

local sample_mapping="$(curl -XGET '${search_host}/${index}/_mapping')"

where search_host and index are two variables previously set. This command fails because Couldn't resolve host '$search_host'

The other posts didn't talk about what to do when dealing with variables. Anyone has any idea?

John1024

This is the command in your command substitution:

curl -XGET '${search_host}/${index}/_mapping'

This command will not work on its own. The problem that you are observing is due to the single quotes, not to the use of command substitution, $(...).

Inside single-quotes, variables are not expanded. Since you need variable expansion, use double-quotes:

curl -XGET "${search_host}/${index}/_mapping"

Putting the above in your original command:

local sample_mapping="$(curl -XGET "${search_host}/${index}/_mapping")"

Note that quotes and command substitutions can nest : the fact that there are double quotes inside the command substitution has no effect on the double quotes outside of the command substitution.

Example

Let's define some variables:

$ search_host=http://google.com
$ index=index.html

Now, let's try the command with single-quotes:

$ a=$(curl -XGET '$search_host/$index')
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: $search_host

The above failed with an error message similar to the one you observed: Could not resolve host: $search_host

Let's try again with double-quotes:

$ a=$(curl -XGET "$search_host/$index")
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   229  100   

229    0     0    520      0 --:--:-- --:--:-- --:--:--   521

The above succeeded.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shell script cUrl command with URL being a variable

From Dev

How to loop over shell script, with different variable, store entire contents of script with variable, and then run another function

From Dev

How can I set the value of an AppleScript variable to the result of a shell script's cURL request?

From Dev

how to store result of tail command in variable?

From Dev

Store the result of a command in a variable

From Dev

How to store big values in shell script variable

From Dev

How to store the output of shell script into variable

From Dev

Trying to use Applescript variable in curl command inside do shell script

From Dev

How to use shell script variable in mv command

From Dev

How to append a variable in SCP command in shell script?

From Dev

Command curl malformed in shell script

From Dev

How to pass shell variables as Command Line Argument to a shell script

From Dev

How to pass shell variables as Command Line Argument to a shell script

From Dev

Store Result of SVN Command in Variable

From Dev

How to get the result of a grep command on a variable into another variable

From Dev

How to split a variable and store the result of the same in different variables in tcl

From Dev

Store value to variable in Shell script

From Dev

How to pass date (or in general the result of a shell script) to command in supervisord

From Dev

How to pass date (or in general the result of a shell script) to command in supervisord

From Dev

How to store elapsed time of a variable assignment to another variable in bash script?

From Dev

How to save command result in variable in Jenkins shell build step

From Dev

How to store the result of the expression into a variable in Windows batch script?

From Dev

how to assign one variable value into another variable in shell script

From Dev

How do I set an environment variable in the shell to be result of python script?

From Dev

how to save awk result from hadoop to a variable in shell script?

From Dev

Perl command-line script and shell variables: How to quote?

From Dev

How to assign variables with Command results with HEREDOC in shell script

From Dev

How to pass shell variables to a command that reads console input in a script?

From Dev

How to pass variables as parameters to Bigquery command line in shell script

Related Related

  1. 1

    Shell script cUrl command with URL being a variable

  2. 2

    How to loop over shell script, with different variable, store entire contents of script with variable, and then run another function

  3. 3

    How can I set the value of an AppleScript variable to the result of a shell script's cURL request?

  4. 4

    how to store result of tail command in variable?

  5. 5

    Store the result of a command in a variable

  6. 6

    How to store big values in shell script variable

  7. 7

    How to store the output of shell script into variable

  8. 8

    Trying to use Applescript variable in curl command inside do shell script

  9. 9

    How to use shell script variable in mv command

  10. 10

    How to append a variable in SCP command in shell script?

  11. 11

    Command curl malformed in shell script

  12. 12

    How to pass shell variables as Command Line Argument to a shell script

  13. 13

    How to pass shell variables as Command Line Argument to a shell script

  14. 14

    Store Result of SVN Command in Variable

  15. 15

    How to get the result of a grep command on a variable into another variable

  16. 16

    How to split a variable and store the result of the same in different variables in tcl

  17. 17

    Store value to variable in Shell script

  18. 18

    How to pass date (or in general the result of a shell script) to command in supervisord

  19. 19

    How to pass date (or in general the result of a shell script) to command in supervisord

  20. 20

    How to store elapsed time of a variable assignment to another variable in bash script?

  21. 21

    How to save command result in variable in Jenkins shell build step

  22. 22

    How to store the result of the expression into a variable in Windows batch script?

  23. 23

    how to assign one variable value into another variable in shell script

  24. 24

    How do I set an environment variable in the shell to be result of python script?

  25. 25

    how to save awk result from hadoop to a variable in shell script?

  26. 26

    Perl command-line script and shell variables: How to quote?

  27. 27

    How to assign variables with Command results with HEREDOC in shell script

  28. 28

    How to pass shell variables to a command that reads console input in a script?

  29. 29

    How to pass variables as parameters to Bigquery command line in shell script

HotTag

Archive