I came across a bash file which has something like this
MYVAR := SomeVal
I know after reading some tutorials you set values as such
MYVAR = SomeVal
What is the difference between the above two ?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityI came across a bash file which has something like this
MYVAR := SomeVal
I know after reading some tutorials you set values as such
MYVAR = SomeVal
What is the difference between the above two ?
You probably saw a parameter expansion to set a default value.
The difference is whether the variable is unset or it is set with a null value:
when the variable is unset, there's no difference
$ unset MYVAR; : ${MYVAR:=SomeVal}; echo "$MYVAR"
SomeVal
$ unset MYVAR; : ${MYVAR=SomeVal}; echo "$MYVAR"
SomeVal
when the variable has a value, there's no difference
$ MYVAR="foo"; : ${MYVAR:=SomeVal}; echo "$MYVAR"
foo
$ MYVAR="foo"; : ${MYVAR=SomeVal}; echo "$MYVAR"
foo
when the variable has null value:
$ MYVAR=""; : ${MYVAR:=SomeVal}; echo "$MYVAR"
SomeVal
$ MYVAR=""; : ${MYVAR=SomeVal}; echo "$MYVAR"
# empty line