Where is documentation for [ x$feature_... = xy ] in grub.cfg?

feature_platform_search_hint (and others) and the shell-like syntax are a feature of the normal module in grub. The syntax is somewhat documented in the GRUB manual - although I couldn't find documentation for [] The script language is "obviously" modelled after bash, as it reserves words used in bash even if not yet implemented (such as [[) and implements bashisms such as function.

The predefined variables such as feature_platform_search_hint are hardcoded in the normal module at build time, and are essentially undocumented except by studying the source code. In current versions of grub, these variables are defined to be y at all times, so these variables seem to indicate support for specific features of a given grub version, and testing them can be done to write grub.cfg files that work with multiple versions of grub.

For example, feature_platform_search_hint=y seems to indicate that the search command supports --hint-bios, --hint-efi and so on switches, while the absence of this variable might indicate a version of grub that doesn't support these switches, so you shouldn't try to execute a search statement using them to avoid syntax errors.

There is a lot of conjecture and "may" in my answer, as all of this seems to be essentially undocumented.


The syntax is described in the GRUB manual as "a syntax quite similar to that of GNU Bash and other Bourne shell derivatives". Quoting and variable expansion work similar as in the shell. The opening bracket "[" is a synonym for GRUB's test builtin command.

The list of features is maintained as features global variable in normal/main.c. feature_platform_search_hint has been available since GRUB 2.00 (released 2012). So the conditional code checking for this feature is pointless in all modern Linux distributions.

At the time of this writing, the latest feature added was feature_timeout_style, available since 2.02-beta1.


Strictly speaking, expression wise, if [ x$feature_platform_search_hint = xy ] has nothing to do with grub, it's a shell expression, to be interpreted by shell. grub is using it in one of it's helper script, that's it.

if [ x$feature_platform_search_hint = xy ] is basically testing if the variable feature_platform_search_hint is expanded to y.

How?

The [ is a synonym for command test (can be a shell builtin or an external command), used for evaluating expressions.

In if [ x$feature_platform_search_hint = xy ]:

  • [ is testing whether strings x$feature_platform_search_hint and xy are same

  • here x is a placeholder, dummy string, exists on both sides

  • the variable $feature_platform_search_hint is expanded first, the value is added to already existing string x, and then the string on left is compared to the string on the right of =

  • In effect, it is necessarily checking if variable feature_platform_search_hint has value y

  • Here x is used so that if the variable feature_platform_search_hint is unset or null, then the [ will exit with an error as = requires argument on both side, and without feature_platform_search_hint, this will become:

    if [ = y ]
    

    using x gets us syntactically correct usage in that case:

    if [ x = xy ]
    

Note that, one should instead use the -z test (which tests if the string is of zero length), or -n test (non-zero length test), whichever suits best:

if [ -n "$feature_platform_search_hint" ]
if [ -z "$feature_platform_search_hint" ] 

One should also quote variables (although strictly not necessary in this case as presumably the author intended to define/overwrite the variable inside the script only):

if [ x"$feature_platform_search_hint" = xy ]