1

Can I put comments ( commented out code or notes that don't affect the script ) inside of single quotes within a shell script?

I have tried the following to get these to work:

# comment
\# comment
\/\* comment \*\/

Below is an example of what I'm trying to do. You'll see my comments start with a hash and are inside single quotes withint the curl request's data package.

curl -XPOST 'http://ec2-54-234-64-66.compute-1.amazonaws.com:9200/nsns/nsn/_mapping?pretty=true' -d '{
    "nsn" : {

        "type" : "object",
        "include_in_all" : "false",
        "index" : "no",
        "properties" : {

        # this data is used for facetting END

            "id" : { "type" : "long", "include_in_all" : "true" },
            "BDC_CODE" : { "type" : "byte" },
            "KID" : { "type" : "byte" },
            "ITEM_STANDARDIZATION_CODE" : { "type" : "string" },

        # we don't know what these field's data look like yet

            "stock_on_hand" : { "type" : "integer" },
            "non_stocked_items" : { "type" : "integer" },
            "stocked_restrictions" : { "type" : "string" },
            "consistency_of_spend" : { "type" : "string" },
            "WSDC" : { "type" : "string" }

        }
    }
}'
T. Brian Jones
  • 13,002
  • 25
  • 78
  • 117

2 Answers2

2

The problem is not single quotes but using JSON. AFAIK, you can't add comments in JSON, see Can comments be used in JSON?

Note :

you should add -H "Content-Type: text/json" in your cURL

Community
  • 1
  • 1
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

You can't put them inside the quotes, but you can break the string up so the comments can stay:

json='{
    "nsn" : {

        "type" : "object",
        "include_in_all" : "false",
        "index" : "no",
        "properties" : {
'

# this data is used for facetting END
json+='
            "id" : { "type" : "long", "include_in_all" : "true" },
            "BDC_CODE" : { "type" : "byte" },
            "KID" : { "type" : "byte" },
            "ITEM_STANDARDIZATION_CODE" : { "type" : "string" },
'

# we don't know what these field's data look like yet
json+='
            "stock_on_hand" : { "type" : "integer" },
            "non_stocked_items" : { "type" : "integer" },
            "stocked_restrictions" : { "type" : "string" },
            "consistency_of_spend" : { "type" : "string" },
            "WSDC" : { "type" : "string" }
        }
    }
}'

url='http://ec2-54-234-64-66.compute-1.amazonaws.com:9200/nsns/nsn/_mapping?pretty=true'

curl -XPOST "$url" -d "$json"

Note that += is a bash-specific string concatenating assignment operator.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352