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.

2
  • 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 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 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 at 18:31

1 Answer 1

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\""
7
  • 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 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 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 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 at 5: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 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.