square / connect-api-specification

This repository contains the OpenAPI specification as well as templates for generating SDKs for Square's APIs
https://squareup.com/developers
Apache License 2.0
72 stars 35 forks source link

What is the purpose of `+1` in the generate-sdks script? #138

Closed Cliabhach closed 5 years ago

Cliabhach commented 5 years ago

I noticed that line 5 of https://github.com/square/connect-api-specification/blob/10b866c654/generate-sdks uses an unusual form of parameter expansion:

if [ -n "${SWAGGER_CMD+1}" ]

I had trouble finding documentation on what the + syntax means. Is there any practical difference between that and the below?

if [ -n "${SWAGGER_CMD}" ]
havenwood commented 5 years ago

The difference is only that the former is truthy for an empty string and the latter is not. It's a shorthand to check if a variable is set, allowing for it to be set but empty.

empty=""

[[ -n "${empty+1}" ]] && echo "true" || echo "false"
# >> true

[[ -n "${empty}" ]] && echo "true" || echo "false"
# >> false
Cliabhach commented 5 years ago

Ah, I see. If it's an empty string, the script skips the installation request and goes straight on to the invocation(s) of generate_for_language(). Thanks for explaining!