3

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
2
  • You don't run bash in your example, you run sh. Commented Dec 18, 2017 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. Commented Dec 18, 2017 at 11:14

2 Answers 2

5

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

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}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .