How to set variable in the curl command in bash?

How to set variable in the curl command in bash?



I have a bash file:


#!/bin/bash

# yesnobox.sh - An inputbox demon shell script
OUTPUT="/tmp/input.txt"

# create empty file
>$OUTPUT

# cleanup - add a trap that will remove $OUTPUT
# if any of the signals - SIGHUP SIGINT SIGTERM it received.
trap "rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM

# show an inputbox
dialog --title "Inputbox"
--backtitle "Search vacancies"
--inputbox "Enter your query " 8 60 2>$OUTPUT

# get respose
respose=$?

# get data stored in $OUPUT using input redirection
name=$(<$OUTPUT)

curl -d '"query":"developer", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer



in last string (curl command) I want to set variable name instead "developer". How to correctly insert it?





Sidenote: you can create a temporary file like so: OUTPUT=$(mktemp). It will create a unique file (sth. like /tmp/tmp.RyR3UlsV5c). However, you will still need to delete it manually, like you already do. mktemp creates such a file and returns (prints) its filename.
– PerlDuck
Aug 31 at 12:55


OUTPUT=$(mktemp)


/tmp/tmp.RyR3UlsV5c


mktemp




4 Answers
4



To access variables, you have to put a dollar sign in front of the name: $name


$name



However, variables do not get expanded inside strings enclosed in 'single quotes'. You should have them wrapped inside "double quotes" though, to prevent word splitting of the expanded value, if it might contain spaces.



So there are basically two ways, we either put the whole argument in double quotes to make the variable expandable, but then we have to escape the double quote characters inside, so that they end up in the actual parameter (command line shortened):


curl -d ""query":"$name", "turnOff":true" ...



Alternatively, we can concatenate string literals enclosed in different quote types by writing them immediately next to each other:


curl -d '"query":"'"$name"'", "turnOff":true' ...



Since the value for curls -d parameter is within single quotes means that there will be no parameter expansion, just adding the variable would not work. You can get around this by ending the string literal, adding the variable and then starting the string literal again:


-d


curl -d '"query":"'"$name"'", "turnOff":true' -H "Content-Type: application/json" -X POST http://localhost:8080/explorer



The extra double quotes around the variable are used to prevent unwanted shell parameter expansion.



@ByteCommander's answer is good, assuming you know that the value of name is a properly escaped JSON string literal. If you can't (or don't want to) make that assumption, use a tool like jq to generate the JSON for you.


name


jq


curl -d "$(jq -n --arg n "$name" 'query: $n, turnOff: true')"
-H "Content-Type: application/json" -X POST http://localhost:8080/explorer



Some might find this more readable and maintainable since it avoids using escaping, and sequences of single and double quotes, which are hard to follow and match.



Use Bash's equivalent of sprintf to template the substitution:


sprintf


printf -v data '"query":"%s", "turnOff":true' "developer"

curl -d "$data" -H "Content-Type: application/json" -X POST http://localhost:8080/explorer



Thanks for contributing an answer to Ask Ubuntu!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế