How variables inside braces are evaluated












5















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










share|improve this question




















  • 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
















5















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










share|improve this question




















  • 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














5












5








5


2






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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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










1 Answer
1






active

oldest

votes


















6














These are all various forms of parameter expansion with alternatives:





  • ${var:-val} is replaced by val if var is unset or null, ${var} otherwise (so val is a "default value");


  • ${var:=val} first assigns val to var if var is unset or null, and then (in all cases) is replaced by ${var};


  • ${var:+val} is replaced with nothing if var 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 :.






share|improve this answer


























  • 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













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
});


}
});














draft saved

draft discarded


















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









6














These are all various forms of parameter expansion with alternatives:





  • ${var:-val} is replaced by val if var is unset or null, ${var} otherwise (so val is a "default value");


  • ${var:=val} first assigns val to var if var is unset or null, and then (in all cases) is replaced by ${var};


  • ${var:+val} is replaced with nothing if var 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 :.






share|improve this answer


























  • 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


















6














These are all various forms of parameter expansion with alternatives:





  • ${var:-val} is replaced by val if var is unset or null, ${var} otherwise (so val is a "default value");


  • ${var:=val} first assigns val to var if var is unset or null, and then (in all cases) is replaced by ${var};


  • ${var:+val} is replaced with nothing if var 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 :.






share|improve this answer


























  • 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
















6












6








6







These are all various forms of parameter expansion with alternatives:





  • ${var:-val} is replaced by val if var is unset or null, ${var} otherwise (so val is a "default value");


  • ${var:=val} first assigns val to var if var is unset or null, and then (in all cases) is replaced by ${var};


  • ${var:+val} is replaced with nothing if var 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 :.






share|improve this answer















These are all various forms of parameter expansion with alternatives:





  • ${var:-val} is replaced by val if var is unset or null, ${var} otherwise (so val is a "default value");


  • ${var:=val} first assigns val to var if var is unset or null, and then (in all cases) is replaced by ${var};


  • ${var:+val} is replaced with nothing if var 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 :.







share|improve this answer














share|improve this answer



share|improve this answer








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





















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?