Bash script string interpolation leaves curly braces intact

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
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
bash shell-script
add a comment |Â
up vote
2
down vote
favorite
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
bash shell-script
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$versionis replaced, because variable expansion is one the things which does get performed within double-quoted strings.
â user1934428
Dec 18 '17 at 11:14
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
bash shell-script
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
bash shell-script
asked Dec 15 '17 at 20:02
Alexei Danchenkov
207311
207311
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$versionis replaced, because variable expansion is one the things which does get performed within double-quoted strings.
â user1934428
Dec 18 '17 at 11:14
add a comment |Â
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$versionis replaced, because variable expansion is one the things which does get performed within double-quoted strings.
â user1934428
Dec 18 '17 at 11:14
You don't run bash in your example, you run sh.
â user1934428
Dec 18 '17 at 11:12
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
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
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
The { is before $. It should be $version :)
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
The { is before $. It should be $version :)
add a comment |Â
up vote
3
down vote
accepted
The { is before $. It should be $version :)
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
The { is before $. It should be $version :)
The { is before $. It should be $version :)
answered Dec 15 '17 at 20:07
m0dular
63115
63115
add a comment |Â
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
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.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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.
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.
answered Dec 15 '17 at 20:07
John1024
44.2k4100116
44.2k4100116
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411122%2fbash-script-string-interpolation-leaves-curly-braces-intact%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$versionis replaced, because variable expansion is one the things which does get performed within double-quoted strings.â user1934428
Dec 18 '17 at 11:14