Creating custom menu entry, got stuck on this command:
exec tail -n +3 $0
Tried it in terminal, got weird result, cannot understand, what this command exactly does and why grub needs it. Could you explain, please?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
If you're talking about
$ cat /etc/grub.d/40_custom #!/bin/sh exec tail -n +3 $0 # This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above.
Then note that:
grub-mkconfigto build GRUB configuration
But this is a shell script, so usually you'd have to do something like
echo "menuentry ...." etc. To avoid that, the
exec tail magic is used. What does that do?
$0, remember, is the name of the script as executed, so typically it would be
/etc/grub.d/40_custom, etc. depending on where and how it was run). So the script is essentially running
tail on itself, but with
-n +3, which tells
tail to start from the third line.
What do you get if you output everything from the third line onwards in
# This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above.
(And additionally whatever else you put below this.)
exec part replaces the shell that's executing the script with
tail, so effectively nothing further from the script is executed.
Running it in the terminal:
bashor something like that (it could be
exec, you're replacing the running shell with
tail -n+3 bash
bashin your current directory,
So the end result is likely that your terminal session ended there.
tail -n +3 prints its input, starting at line 3 (man page).
$0 is the name of the script in a shell script (Bash special parameters) and
exec (Bash builtins) replaces the script with the command. You probably have something like this (like in
/etc/grub.d/40_custom on my system):
#!/bin/sh exec tail -n +3 $0 foo bar
When you run the script, it replaces itself with
tail reading the script itself, so the rest of the script gets copied to its output.
I think grub has a bunch of scripts to create its config, they're probably executed as
grubscript.sh >> grub-config-file or something to effect. The scripts could use any logic they need to produce the output, but the
exec tail trick allows to just dump some fixed lines in the output without changing the logic the script is started with.
In addition to that magic incantation, Debian's
/etc/grub.d/40_custom also includes a comment telling the user to
Simply type the menu entries you want to add after this comment.