Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash string interpolation returns empty string [duplicate]

Tags:

linux

bash

I am working on simple curl POST script and I have problem with string interpolation of a variable that is initialized with data from a script. Let me describe it:

#!/bin/bash

EMAIL=$(source ./random_email_generator.sh) #this generates some random string 
echo "$EMAIL" #here email is printed well
curl --insecure --header "Content-Type: application/json" \
  --request POST \
  --data '{"email":'"$EMAIL"',"password":"validPassword"}' \ #problem lies here because email is always empty string 
 http:/ticketing.dev/api/users/signup 

To sum up, there is a lot of quotes and double quotes here and it is abit mixed up, how can I resolve value of EMAIL in place of json body?

Thanks in advance

like image 344
Mateusz Gebroski Avatar asked Apr 21 '26 19:04

Mateusz Gebroski


1 Answers

The variable is being interpolated properly. The problem is that you're not wrapping it in double quotes, so you're creating invalid JSON.

curl --insecure --header "Content-Type: application/json" \
  --request POST \
  --data '{"email":"'"$EMAIL"'","password":"validPassword"}' \
  http:/ticketing.dev/api/users/signup 

When you want to see what the command actually looks like, put echo before curl or run the script with bash -x. You would have seen

--data {"email":[email protected],"password":"validPassword"}

It would be better if you used the jq tool to manipulate JSON in bash scripts.

like image 145
Barmar Avatar answered Apr 24 '26 19:04

Barmar