2

The request string in the example below interpolates version variable, but keeps the curly braces and I can't figure out why.

#!/bin/sh

version=2989
request="http://example.com/?version={$version}&therest"
echo "$request"

Result:

$ ~/script.sh
http://example.com/?version={2989}&therest

Environment:

$ echo $0
-zsh
  • You don't run bash in your example, you run sh. – user1934428 Dec 18 '17 at 11:12
  • You have the parenthesis inside a quoted string, so they are output literally. Only $version is replaced, because variable expansion is one the things which does get performed within double-quoted strings. – user1934428 Dec 18 '17 at 11:14
3

The { is before $. It should be ${version} :)

| improve this answer | |
1

Compare:

$ version=1.2; echo "http://example.com/?version={$version}&therest"
http://example.com/?version={1.2}&therest
$ version=1.2; echo "http://example.com/?version=${version}&therest"
http://example.com/?version=1.2&therest

Inside quoted, braces are regarded as just normal characters unless they are part of some construct like ${variableName}.

| improve this answer | |

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.