Basic

Just like POSIX shell, GRUB scripting language has almost all the similarity. However, you should be familiar with the POSIX shell before engaging Grub scripting language. Otherwise, it will be tough to cope it backwards. In this section, we cover the basic syntax of the scripts.

Comments

Anything starts with a hash (#) is a comment. Everything after that is no longer considered an effective codes, be it full line or inline. Example:

# this is a full line comment
for i in (hd0,msdos1)/*; do # this is an inline comment
        ...
done

Quoting

There are 2 types of quoting:

  • single quote (') - preserve every characters as it is, ignoring its representation, making it a complete string.
  • double quotes (") - interpret special characters like ($ and \) for the given string.

Example:

set core='This is a confirmed string, negating backslash and dollar sign, so that \n means 2 characters - backslash and n'

set dyna="This is an interpreted string, interpret both backslash and dollar sign, so that the newline character is interpreted, as in \n"

Variable Expansion

The dollar sign ($) is an variable expansion. A variable start with alphabet following with alphanumeric characters or underscore (_). We will review all CRUD of the variables. Unlike POSIX shell, you can't perform variable manipulations in the variable expansion. It was meant for separate variable name and other string characters from misinterpretation.


Create (C)

To create, you can use 2 ways:

# directly set the variable (commonly use)
hello_world_="Hello reader! I'm a string!"

# use the set command
set hello_world_="Hello reader! I'm a string"


Read (R)

To read, you can:

# use the dollar sign and represent it
grub> echo "$hello_world"

# use variable expansion
grub> echo "${hello_world}"


Update (U)

To update, you can just recreate again.


Delete (D)

To delete, use unset command and provide the variable name

grub> unset hello_world

Reserved Variables

There are a lot of reserved variables mentioned in the specification. Keep in mind not to collide with them as you create your own variable. If in doubt, start with underscore as the first character of the name.

That's all about the basic for grub.cfg.