9

I can't find any reference of x$feature... using Google as it will show me examples of /boot/grub/grub.cfg.

In self-generated grub.cfg, there is a lot of

if [ x$feature_platform_search_hint = xy ] ...

if [ x$feature_default_font_path = xy]...

if [ x$feature_all_video_module = xy ]...

My understanding is that it is checking if the feature is present. Where can I find every feature available with description.

I assume xy means something like true or available. What other values can you have?

Edit: I only need to know the variables available in /boot/grub/grub.cfg. Preferably a documentation.

2

3 Answers 3

7

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.

7

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.

1
5

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 ]
7
  • I didn't know you could write variables next to each other in bash without the quotes. I guess the double quotes are for weak quoting only. Is there a documentation for the variables available in the helper script.
    – Rami
    Oct 5, 2016 at 6:28
  • @Rami double quotes are used to avoid word-splitting, i.e. when you want contents of your variable to be treated as one entity. With quotes variable $VAR may expand to one single entity "hello world", but without quotes it may be treated as two - "hello" and "world". Sometimes it's desirable, but often it is not desirable to have word splitting Oct 5, 2016 at 6:38
  • @Serg I knew that. I just didn't know that you can write something like somecharacters$VAR1$VAR2 (without spaces). I assumed it was like php. You need quotes to write variables next to each other with no space in php.
    – Rami
    Oct 5, 2016 at 6:59
  • 7
    While this answer provies nice detail on the conjectured syntax of the if statement and test expression ("[]") it unfortunately completely misses the target, as the quetsioon is about GRUB, and neither the if statement not the symbols are interpreted by helper scripts, but are interpreted by grub (as the original poster remarks, it is in grub.cfg, which is interpreted at boot time when no (unix) shell is available) Jul 15, 2017 at 23:06
  • 2
    As already mentioned by @RememberMonica the very first sentence of the answer is so terribly wrong. The expression is being executed by GRUB. It is not for any shell. The GRUB itself is has a bash like script processor internally. Some people even say that it starts to become a tiny OS on its own. :) Jan 21, 2021 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.