How variables inside braces are evaluated

Multi tool use
I'm new to shell scripting and I came across these expressions
${var:-val}
and ${var-val}
${var:+val}
and ${var+val}
${var:=val}
and ${var=val}
so how are they evaluated and what are the differences between them
bash shell scripting
add a comment |
I'm new to shell scripting and I came across these expressions
${var:-val}
and ${var-val}
${var:+val}
and ${var+val}
${var:=val}
and ${var=val}
so how are they evaluated and what are the differences between them
bash shell scripting
3
IMO, not a duplicate. the linked answer only answers 1/3 of this question.
– cas
May 30 '16 at 3:06
What linked answer?
– fpmurphy
May 30 '16 at 9:25
@fpmurphy1 What does:-
mean in a shell script.
– Stephen Kitt
May 30 '16 at 9:31
add a comment |
I'm new to shell scripting and I came across these expressions
${var:-val}
and ${var-val}
${var:+val}
and ${var+val}
${var:=val}
and ${var=val}
so how are they evaluated and what are the differences between them
bash shell scripting
I'm new to shell scripting and I came across these expressions
${var:-val}
and ${var-val}
${var:+val}
and ${var+val}
${var:=val}
and ${var=val}
so how are they evaluated and what are the differences between them
bash shell scripting
bash shell scripting
edited Feb 12 at 5:25
Rui F Ribeiro
40.5k1479137
40.5k1479137
asked May 29 '16 at 21:39


WLIONWLION
1553
1553
3
IMO, not a duplicate. the linked answer only answers 1/3 of this question.
– cas
May 30 '16 at 3:06
What linked answer?
– fpmurphy
May 30 '16 at 9:25
@fpmurphy1 What does:-
mean in a shell script.
– Stephen Kitt
May 30 '16 at 9:31
add a comment |
3
IMO, not a duplicate. the linked answer only answers 1/3 of this question.
– cas
May 30 '16 at 3:06
What linked answer?
– fpmurphy
May 30 '16 at 9:25
@fpmurphy1 What does:-
mean in a shell script.
– Stephen Kitt
May 30 '16 at 9:31
3
3
IMO, not a duplicate. the linked answer only answers 1/3 of this question.
– cas
May 30 '16 at 3:06
IMO, not a duplicate. the linked answer only answers 1/3 of this question.
– cas
May 30 '16 at 3:06
What linked answer?
– fpmurphy
May 30 '16 at 9:25
What linked answer?
– fpmurphy
May 30 '16 at 9:25
@fpmurphy1 What does
:-
mean in a shell script.– Stephen Kitt
May 30 '16 at 9:31
@fpmurphy1 What does
:-
mean in a shell script.– Stephen Kitt
May 30 '16 at 9:31
add a comment |
1 Answer
1
active
oldest
votes
These are all various forms of parameter expansion with alternatives:
${var:-val}
is replaced byval
ifvar
is unset or null,${var}
otherwise (soval
is a "default value");
${var:=val}
first assignsval
tovar
ifvar
is unset or null, and then (in all cases) is replaced by${var}
;
${var:+val}
is replaced with nothing ifvar
is unset or null,val
otherwise.
Omitting the :
drops the "or null" part of all these definitions.
This is all described in the bash(1)
manpage, and in POSIX.
Some examples might help:
unset a
echo "${a:-default}"
produces default
, as does echo "${a-default}"
.
a=
echo "${a:-default}"
again produces default
, but echo "${a-default}"
outputs a blank line.
a=test
echo "${a:-default}"
produces test
, as does echo "${a-default}"
.
unset a
echo "${a:=default}"
produces default
, and a
is now default
(as confirmed by echo "${a}"
).
The +
form might seem strange, but it is useful when constructing variables in several steps:
PATH="${PATH}${PATH:+:}/blah/bin"
will add :
before /blah/bin
only if PATH
is non-empty, which avoids having a path starting with :
.
The:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g.grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use$ignorecase
unquoted on the cmd line...which is only safe if you know exactly what$ignorecase
can contain.
– cas
May 30 '16 at 12:47
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f286335%2fhow-variables-inside-braces-are-evaluated%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
These are all various forms of parameter expansion with alternatives:
${var:-val}
is replaced byval
ifvar
is unset or null,${var}
otherwise (soval
is a "default value");
${var:=val}
first assignsval
tovar
ifvar
is unset or null, and then (in all cases) is replaced by${var}
;
${var:+val}
is replaced with nothing ifvar
is unset or null,val
otherwise.
Omitting the :
drops the "or null" part of all these definitions.
This is all described in the bash(1)
manpage, and in POSIX.
Some examples might help:
unset a
echo "${a:-default}"
produces default
, as does echo "${a-default}"
.
a=
echo "${a:-default}"
again produces default
, but echo "${a-default}"
outputs a blank line.
a=test
echo "${a:-default}"
produces test
, as does echo "${a-default}"
.
unset a
echo "${a:=default}"
produces default
, and a
is now default
(as confirmed by echo "${a}"
).
The +
form might seem strange, but it is useful when constructing variables in several steps:
PATH="${PATH}${PATH:+:}/blah/bin"
will add :
before /blah/bin
only if PATH
is non-empty, which avoids having a path starting with :
.
The:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g.grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use$ignorecase
unquoted on the cmd line...which is only safe if you know exactly what$ignorecase
can contain.
– cas
May 30 '16 at 12:47
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
add a comment |
These are all various forms of parameter expansion with alternatives:
${var:-val}
is replaced byval
ifvar
is unset or null,${var}
otherwise (soval
is a "default value");
${var:=val}
first assignsval
tovar
ifvar
is unset or null, and then (in all cases) is replaced by${var}
;
${var:+val}
is replaced with nothing ifvar
is unset or null,val
otherwise.
Omitting the :
drops the "or null" part of all these definitions.
This is all described in the bash(1)
manpage, and in POSIX.
Some examples might help:
unset a
echo "${a:-default}"
produces default
, as does echo "${a-default}"
.
a=
echo "${a:-default}"
again produces default
, but echo "${a-default}"
outputs a blank line.
a=test
echo "${a:-default}"
produces test
, as does echo "${a-default}"
.
unset a
echo "${a:=default}"
produces default
, and a
is now default
(as confirmed by echo "${a}"
).
The +
form might seem strange, but it is useful when constructing variables in several steps:
PATH="${PATH}${PATH:+:}/blah/bin"
will add :
before /blah/bin
only if PATH
is non-empty, which avoids having a path starting with :
.
The:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g.grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use$ignorecase
unquoted on the cmd line...which is only safe if you know exactly what$ignorecase
can contain.
– cas
May 30 '16 at 12:47
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
add a comment |
These are all various forms of parameter expansion with alternatives:
${var:-val}
is replaced byval
ifvar
is unset or null,${var}
otherwise (soval
is a "default value");
${var:=val}
first assignsval
tovar
ifvar
is unset or null, and then (in all cases) is replaced by${var}
;
${var:+val}
is replaced with nothing ifvar
is unset or null,val
otherwise.
Omitting the :
drops the "or null" part of all these definitions.
This is all described in the bash(1)
manpage, and in POSIX.
Some examples might help:
unset a
echo "${a:-default}"
produces default
, as does echo "${a-default}"
.
a=
echo "${a:-default}"
again produces default
, but echo "${a-default}"
outputs a blank line.
a=test
echo "${a:-default}"
produces test
, as does echo "${a-default}"
.
unset a
echo "${a:=default}"
produces default
, and a
is now default
(as confirmed by echo "${a}"
).
The +
form might seem strange, but it is useful when constructing variables in several steps:
PATH="${PATH}${PATH:+:}/blah/bin"
will add :
before /blah/bin
only if PATH
is non-empty, which avoids having a path starting with :
.
These are all various forms of parameter expansion with alternatives:
${var:-val}
is replaced byval
ifvar
is unset or null,${var}
otherwise (soval
is a "default value");
${var:=val}
first assignsval
tovar
ifvar
is unset or null, and then (in all cases) is replaced by${var}
;
${var:+val}
is replaced with nothing ifvar
is unset or null,val
otherwise.
Omitting the :
drops the "or null" part of all these definitions.
This is all described in the bash(1)
manpage, and in POSIX.
Some examples might help:
unset a
echo "${a:-default}"
produces default
, as does echo "${a-default}"
.
a=
echo "${a:-default}"
again produces default
, but echo "${a-default}"
outputs a blank line.
a=test
echo "${a:-default}"
produces test
, as does echo "${a-default}"
.
unset a
echo "${a:=default}"
produces default
, and a
is now default
(as confirmed by echo "${a}"
).
The +
form might seem strange, but it is useful when constructing variables in several steps:
PATH="${PATH}${PATH:+:}/blah/bin"
will add :
before /blah/bin
only if PATH
is non-empty, which avoids having a path starting with :
.
edited Feb 12 at 8:16
answered May 30 '16 at 9:26
Stephen KittStephen Kitt
173k24396471
173k24396471
The:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g.grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use$ignorecase
unquoted on the cmd line...which is only safe if you know exactly what$ignorecase
can contain.
– cas
May 30 '16 at 12:47
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
add a comment |
The:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g.grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use$ignorecase
unquoted on the cmd line...which is only safe if you know exactly what$ignorecase
can contain.
– cas
May 30 '16 at 12:47
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
The
:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g. grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use $ignorecase
unquoted on the cmd line...which is only safe if you know exactly what $ignorecase
can contain.– cas
May 30 '16 at 12:47
The
:+
form is also useful for avoiding placing the empty string as an argument on a command line, while still properly double-quoting the variable. e.g. grep ${ignorecase:+"$ignorecase"} ...
. The other, worse, alternative is to just use $ignorecase
unquoted on the cmd line...which is only safe if you know exactly what $ignorecase
can contain.– cas
May 30 '16 at 12:47
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
Also, POSIX Parameter Expansions: pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/…
– Kusalananda
Feb 12 at 7:03
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f286335%2fhow-variables-inside-braces-are-evaluated%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
gc505O9,rrr ID1,viuPV2yE3 TC
3
IMO, not a duplicate. the linked answer only answers 1/3 of this question.
– cas
May 30 '16 at 3:06
What linked answer?
– fpmurphy
May 30 '16 at 9:25
@fpmurphy1 What does
:-
mean in a shell script.– Stephen Kitt
May 30 '16 at 9:31