Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

re-assign value to shell variable

Status
Not open for further replies.

kobewins

Programmer
Dec 1, 2005
57
US
I am looking for the better coding to re-assign value to a shell variable, but not simply overwirte.

For example:

var=2
{code block}
var=3 ### I don't want to simply overwirte var
### Instead
: ${var:=3}
{other code}

Is that the out-of-date style? Are there better ways to do it?

Many thanks.
 
Is that the out-of-date style
No, this is something different:
[!]: ${var:=3}[/!]
if var is not set or is null then it is set to 3; the value of var is then substituted.

In your shell's man page pay attention to Parameter substitution

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I should use "Expansion Operator": $(var:+3} instead.
 
var=${var:+3}
is what I exactly wanted.

In this case, I know that var exists and isn't NULL. Then the value of var is reset to 3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top