KansaiRobot
KansaiRobot

Reputation: 10022

shell programming what does this if condition means

I have read some introductory tutorials on bash programming, but the examples they give are fairly simple.

I have a script with the following line

if [[ "${USE_HMI:=OFF}" == "OFF" ]]; then

What does that mean? I can see that it is comparing for equality to a string "OFF" but what confuses me is the ":=" (which I suppose is an assigment) into a comparison...

Can someone help me interpret this?

Upvotes: 0

Views: 24

Answers (1)

Lucas Mior
Lucas Mior

Reputation: 68

it means you are assigning by default the value OFF on the environment variable $USE_HMI. If you don't set the USE_HMI environment variable, the default value will be OFF, and the if statement will be true.

You can quickly test if on your console:

# Null environment variable
$ unset USE_HMI
$ echo ${USE_HMI}

# Print with default value
$ echo ${USE_HMI:=OFF}
OFF

# Set value and print with default value
$ export USE_HMI=ON

# Default value will be ignored because you have set the environment variable
$ echo ${USE_HMI:=OFF}
ON

Upvotes: 3

Related Questions