Search code examples
linuxgrubgrub2

Grub2 every if statement results in true


I'm currently changing my grub2 menu.

I've got this if statement for testing purposes:

set var1 = "foo"
if[ "${var1}" = "bar"]; then
    default=2
fi

No matter what is written in the var1, the if statement always result in true.

set model = "foo"
if[ "${model}" = "bar"]; then
    default=2
else
    default=0
fi

If I also add an else to it, it will execute both lines.

Is it even possible to check the name of an variable in grub2?


Solution

  • Make sure there are spaces around the test operators [ and ].

    Also remove the spaces around the assignment operator in set var1 = "foo".

    Thus:

    set var1="foo"
    if [ "${var1}" = "bar" ] ; then
        default=2
    fi
    

    (I think the space after the ] is not necessary, but it won't hurt either.)


    Presumably, instead of creating a syntax error, Grub just skips over that error to make sure it doesn't crash; although that seems odd (bad) practice. Or, in the context of your actual problem, it may stop and somehow the a result that matches your description of the comparison always evaluating to true.