0

I am using this command:

curl -I "http://localhost:8080/jobname/buildWithParameters?ENVIRONMENT=UPPER_DEV&IP=$HOSTNAME"

I am giving some part of this:

echo '"$HOSTNAME"'

I am expecting output within the quotes: "abcxyz.dev.int".

but I am getting this output.

"$HOSTNAME"

I am not getting the value of my hostname. kindly help with this.

terdon
  • 242,166
Uma
  • 1
  • Are you just asking why the variable doesn't get expanded within that single-quoted string? That's just because that's the way single quotes work. See https://unix.stackexchange.com/q/503013/170373 You can use e.g. echo "\"$HOSTNAME\"" to include literal double-quotes within the double-quoted string. – ilkkachu Jun 13 '22 at 18:30
  • 1
    That curl command looks fine to me, I guess? And you didn't ask anything about it, so do I get it right that works ok for you? – ilkkachu Jun 13 '22 at 18:31

1 Answers1

1

The single quotes mean the variable won't be expanded:

$ echo '$HOSTNAME'
$HOSTNAME

If you need to print a variable with quotes, it's easier (and generally better anyway) to use printf:

$ printf '"%s"\n' "$HOSTNAME"
"myHost"

If you must use echo, you can do something like this:

$ echo "\"$HOSTNAME\""
"myHost"

or this:

$ echo '"'"$HOSTNAME"'"'
"myHost"

I don't see how this is related to the curl command you show, but if you mean you want the quoted $HOSTNAME in the curl command, then it should be enough to quote the entire URL, like this:

curl -I "http://localhost:8080/jobname/buildWithParameters?ENVIRONMENT=UPPER_DEV&IP=$HOSTNAME"

Or, if the server requires the quotes, you can do:

curl -I "http://localhost:8080/jobname/buildWithParameters?ENVIRONMENT=UPPER_DEV&IP='$HOSTNAME'"

But if you really, really need the double quotes, try:

curl -I "http://localhost:8080/jobname/buildWithParameters?ENVIRONMENT=UPPER_DEV&IP=\"$HOSTNAME\""
terdon
  • 242,166
  • I think OP wants HOSTNAME with quotes in the curl command ; the echo is just to tell that HOSTNAME was not getting expanded. I think the issue here is not "echo" or "printf" but expanding HOSTNAME with quotes in curl command. – Prem Jun 13 '22 at 18:45
  • 1
    Hmm, you could be right, @Prem, thanks. It should be easy enough to adapt the options I'd given, but I made it more explicit, just in case. – terdon Jun 13 '22 at 20:57
  • I think they believe they want the hostname in quotes. I don't think the hostname string has to include literal quotes, but who knows, there might be a singular REST endpoint out there that requires this? – Kusalananda Jun 13 '22 at 21:38
  • Why quotes ? It says IP and then uses HOSTNAME ! Maybe somebody is making it "easy" to get HOSTNAME through REST by grabbing the value between quotes with regex, "without being worried" about various other characters ! @Kusalananda [[ PS : Thanks to terdon to make it Explicit ]] – Prem Jun 14 '22 at 05:53
  • @Prem The parameter's value in the URL runs until the end of the URL or until the next & character. The & character can not be part of the hostname (or IP address), so there's no need for quoting. Also, any library that the REST backend might use to access the parameters and their values would not expect the values to be quoted, leaving the unneeded un-quoting process to the developer. – Kusalananda Jun 14 '22 at 06:06
  • @terdon thanks for the quick response!. The below solution is working for my requirement. Thanks a lot!. curl -I "http://localhost:8080/jobname/buildWithParameters?ENVIRONMENT=UPPER_DEV&IP='$HOSTNAME'" – Uma Jun 14 '22 at 06:16
  • I was cryptic in my earlier comment (to avoid casting aspersions on newbie OP) by using words like "easy" & regex & "without being worried", but (1) Q is taking about echo, while Issue is with curl (2) REST API is using IP to get HOSTNAME (3) In general, quotes are unnecessary but OP "believes" on it (4) I "suspect" backend is written by same team who is also Processing the curl output ;;;; With these Indications of the Experience level, I "suspect" OP is unnecessarily parsing QueryString where quotes make it "easy" to use /"(.*)"/ which might seem wrong to Experienced users. @Kusalananda – Prem Jun 14 '22 at 07:28