I have this script:

#!/bin/bash

TOKEN='faketokendgfhjkdhf'
KEYNAME="test_$(date +'%m-%Y')"
echo $KEYNAME
KEYPATH="$HOME/.ssh/${KEYNAME}"
echo $KEYPATH
SSHPHRASE="testphrase123!"

ssh-keygen -t ed25519 
    -f  $KEYPATH 
    -C $KEYNAME 
    -N "$SSHPHRASE"

echo $KEYPATH
cat "$KEYPATH".pub
PUBKEY="$(cat $KEYPATH.pub)"
echo "$PUBKEY"

read -r -d '' SSH_KEY_REQ <<EOF
{"labels":{},
"name":"$KEYNAME",
"public_key":"$(echo "$PUBKEY"})"}
EOF

echo $SSH_KEY_REQ

curl 
    -X POST 
    -H "Authorization: Bearer "$TOKEN"" 
    -H "Content-Type: application/json" 
    -d $SSH_KEY_REQ 
    "https://api.example.com/v1/ssh_keys"


However, when I run the script it fails with

curl: (6) Could not resolve host: AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1
curl: (3) unmatched close brace/bracket in URL position 13:
test_01-2024}"}

So I ran the script with -x in order to see what strings were actually being created, and it seems a ' is being inserted into the string for public_key.

sh -x htest.sh $PARENTPATH/Secret/variables.sh                                                                                                                                                                                     3 ↵
sh -x htest.sh $PARENTPATH/Secret/variables.sh
+ TOKEN=faketokendgfhjkdhf
++ date +%m-%Y
+ KEYNAME=test_01-2024
+ echo test_01-2024
test_01-2024
+ KEYPATH=/home/jam/.ssh/test_01-2024
+ echo /home/jam/.ssh/test_01-2024
/home/jam/.ssh/test_01-2024
+ SSHPHRASE='testphrase123!'
+ ssh-keygen -t ed25519 -f /home/jam/.ssh/test_01-2024 -C test_01-2024 -N 'testphrase123!'
Generating public/private ed25519 key pair.
Your identification has been saved in /home/jam/.ssh/test_01-2024
Your public key has been saved in /home/jam/.ssh/test_01-2024.pub
The key fingerprint is:
SHA256:biX6M/XeeUNeUZCJpDKFXd46MjyvF8rOmUzxTi8aP7A test_01-2024
The key's randomart image is:
+--[ED25519 256]--+
|         o.oo..+ |
|        ...o..o .|
|        o . . . .|
|         +   . . |
|        S B o   .|
|       o ooB.. ..|
|      . ooo=+.o .|
|       oo+EO=o +.|
|        .+O++o+..|
+----[SHA256]-----+
+ echo /home/jam/.ssh/test_01-2024
/home/jam/.ssh/test_01-2024
+ cat /home/jam/.ssh/test_01-2024.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024
++ cat /home/jam/.ssh/test_01-2024.pub
+ PUBKEY='ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024'
+ echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024'
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024
+ read -r -d '' SSH_KEY_REQ
++ echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024}'
+ echo '{"labels":{},"name":"test_01-2024","public_key":"ssh-ed25519' AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI 'test_01-2024}"}'
{"labels":{},"name":"test_01-2024","public_key":"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024}"}
+ curl -X POST -H 'Authorization: Bearer faketokendgfhjkdhf' -H 'Content-Type: application/json' -d '{"labels":{},"name":"test_01-2024","public_key":"ssh-ed25519' AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI 'test_01-2024}"}' https://api.example.com/v1/ssh_keys
curl: (6) Could not resolve host: AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1
curl: (3) unmatched close brace/bracket in URL position 13:
test_01-2024}"}
            ^

The use of read and/or the use of a heredoc seems to be where the unwanted ' comes in:

+ read -r -d '' SSH_KEY_REQ
++ echo 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024}'
+ echo '{"labels":{},"name":"test_01-2024","public_key":"ssh-ed25519' AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI 'test_01-2024}"}'

I am unclear as to how/why this happens. My goal is to use the json string with the unmodified pubkey in SSH_KEY_REQ such that the curl command actually run is:

curl -X POST -H 'Authorization: Bearer faketokendgfhjkdhf' -H 'Content-Type: application/json' -d '{"labels":{},"name":"test_01-2024","public_key":"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMu4pDny1/78PYQBcu6kPs+n2e0Jt9goIyDMqFcndFPI test_01-2024"}' https://api.example.com/v1/ssh_keys