How to list GRUB's “menuentries” in command-line?












21















Is there a way of retrieving list of "menuentries" titles of GRUB?



I need a results in a format that grub-set-default will accept. eg.



"Ubuntu"
"Memory test (memtest86+)"
etc.









share|improve this question

























  • See help.ubuntu.com/community/Grub2/Setup and help.ubuntu.com/community/Grub2

    – Panther
    Mar 20 '15 at 16:53
















21















Is there a way of retrieving list of "menuentries" titles of GRUB?



I need a results in a format that grub-set-default will accept. eg.



"Ubuntu"
"Memory test (memtest86+)"
etc.









share|improve this question

























  • See help.ubuntu.com/community/Grub2/Setup and help.ubuntu.com/community/Grub2

    – Panther
    Mar 20 '15 at 16:53














21












21








21


3






Is there a way of retrieving list of "menuentries" titles of GRUB?



I need a results in a format that grub-set-default will accept. eg.



"Ubuntu"
"Memory test (memtest86+)"
etc.









share|improve this question
















Is there a way of retrieving list of "menuentries" titles of GRUB?



I need a results in a format that grub-set-default will accept. eg.



"Ubuntu"
"Memory test (memtest86+)"
etc.






command-line grub2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 '15 at 17:03







madneon

















asked Mar 20 '15 at 16:50









madneonmadneon

1,4381820




1,4381820













  • See help.ubuntu.com/community/Grub2/Setup and help.ubuntu.com/community/Grub2

    – Panther
    Mar 20 '15 at 16:53



















  • See help.ubuntu.com/community/Grub2/Setup and help.ubuntu.com/community/Grub2

    – Panther
    Mar 20 '15 at 16:53

















See help.ubuntu.com/community/Grub2/Setup and help.ubuntu.com/community/Grub2

– Panther
Mar 20 '15 at 16:53





See help.ubuntu.com/community/Grub2/Setup and help.ubuntu.com/community/Grub2

– Panther
Mar 20 '15 at 16:53










7 Answers
7






active

oldest

votes


















29














Using awk



awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg 


gives you a full list of all menu entries in grub.cfg.






share|improve this answer





















  • 1





    You do not need to pipe grep to awk , awk will pattern match with /foo/

    – Panther
    Mar 20 '15 at 17:06






  • 1





    awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

    – Panther
    Mar 20 '15 at 17:57











  • A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

    – Fabby
    Aug 9 '15 at 20:40











  • @Fabby a note for you. OMG Those were my first steps with awk. :

    – A.B.
    Aug 9 '15 at 21:03






  • 3





    Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

    – mikezter
    Apr 28 '17 at 18:42



















7














Exactly this has been annoying me for over a year - so I did a quick and dirty script to do it. Hope this helps others?



This stacks the names in a push/pop queue, but not the menu indexing so it could be better but I have gone as far as I could be bothered.



gawk  'BEGIN {                                                                                                                       
l=0
menuindex= 0
stack[t=0] = 0
}

function push(x) { stack[t++] = x }

function pop() { if (t > 0) { return stack[--t] } else { return "" } }

{

if( $0 ~ /.*menu.*{.*/ )
{
push( $0 )
l++;

} else if( $0 ~ /.*{.*/ )
{
push( $0 )

} else if( $0 ~ /.*}.*/ )
{
X = pop()
if( X ~ /.*menu.*{.*/ )
{
l--;
match( X, /^[^''']*'''([^''']*)'''.*$/, arr )

if( l == 0 )
{
print menuindex ": " arr[1]
menuindex++
submenu=0
} else
{
print " " (menuindex-1) ">" submenu " " arr[1]
submenu++
}
}
}

}' /boot/grub/grub.cfg


Here you can see a screen grab from my box showing it running
enter image description here






share|improve this answer


























  • +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

    – WinEunuuchs2Unix
    Apr 6 '18 at 23:55





















7














Color Screen shot (short version)



Use mouse scroll wheel, Home, End, PgUp, PgDn, and keys to navigate the menu.



grub-menu.sh



Text Screen shot (long version)



The bash script uses whiptail instead of dialog to display the menu. One advantage is you can copy the terminal image to the clipboard as
text and paste it into this website as text. Other advantages include:




  • Mouse scroll wheel support

  • Faster performance


  • dialog is not installed by default in Ubuntu Server or Lubuntu. whiptail is included by default.


Here's a text screen shot:



Grub Version: 2.02~beta2-36ubuntu3.15


┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
│ Menu No. --------------- Menu Name --------------- │
│ │
│ 0 Ubuntu ↑ │
│ 1 Advanced options for Ubuntu ▮ │
│ 1>0 Ubuntu, with Linux 4.14.31-041431-generic ▒ │
│ 1>1 Ubuntu, with Linux 4.14.31-041431-generic (upstart) ▒ │
│ 1>2 Ubuntu, with Linux 4.14.31-041431-generic (recovery mode) ▒ │
│ 1>3 Ubuntu, with Linux 4.14.30-041430-generic ▒ │
│ 1>4 Ubuntu, with Linux 4.14.30-041430-generic (upstart) ▒ │
│ 1>5 Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) ▒ │
│ 1>6 Ubuntu, with Linux 4.14.27-041427-generic ▒ │
│ 1>7 Ubuntu, with Linux 4.14.27-041427-generic (upstart) ▒ │
│ 1>8 Ubuntu, with Linux 4.14.27-041427-generic (recovery mode) ▒ │
│ 1>9 Ubuntu, with Linux 4.14.24-041424-generic ▒ │
│ 1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart) ▒ │
│ 1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode) ▒ │
│ 1>12 Ubuntu, with Linux 4.14.23-041423-generic ▒ │
│ 1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart) ↓ │
│ │
│ │
│ <Display Grub Boot> <Exit> │
│ │
└──────────────────────────────────────────────────────────────────────────┘


Highlight entry and press Enter



Use the navigation keys to highlight an option and press Enter to see the pre-kernel drivers loaded by grub and the boot parameters passed by grub to the kernel when booting it:



menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
recordfail
savedefault
load_video
gfxmode $linux_gfx_mode
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
else
search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
fi
echo 'Loading Linux 4.14.27-041427-generic ...'
linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
echo 'Loading initial ramdisk ...'
initrd /boot/initrd.img-4.14.27-041427-generic
}
Press <Enter> to continue





grub-menu.sh bash script





grub-menu.sh only has one option to tweak:



# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
HideUpstartRecovery=false


Set the value to true (hide the extra entries) or false (list all entries).



The default format can be overridden when calling the script using:



grub-menu.sh short


or:



grub-menu.sh long


The code:



#!/bin/bash

# NAME: grub-menu.sh
# PATH: $HOME/bin
# DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
# DATE: Apr 5, 2018. Modified: May 7, 2018.

# $TERM variable may be missing when called via desktop shortcut
CurrentTERM=$(env | grep TERM)
if [[ $CurrentTERM == "" ]] ; then
notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
exit 1
fi

AllMenusArr=() # All menu options.
# Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
HideUpstartRecovery=false
if [[ $1 == short ]] ; then
HideUpstartRecovery=true # override default with first passed parameter "short"
elif [[ $1 == long ]] ; then
HideUpstartRecovery=false # override default with first passed parameter "long"
fi
SkippedMenuEntry=false # Don't change this value, automatically maintained
InSubMenu=false # Within a line beginning with `submenu`?
InMenuEntry=false # Within a line beginning with `menuentry` and ending in `{`?
NextMenuEntryNo=0 # Next grub internal menu entry number to assign
# Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
ThisSubMenuMajorNo=0
NextSubMenuMinorNo=0
CurrTag="" # Current grub internal menu number, zero based
CurrText="" # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
SubMenuList="" # Only supports 10 submenus! Numbered 0 to 9. Future use.

while read -r line; do
# Example: " }"
BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
if [[ $BlackLine == "}" ]] ; then
# Add menu option in buffer
if [[ $SkippedMenuEntry == true ]] ; then
NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
SkippedMenuEntry=false
continue
fi
if [[ $InMenuEntry == true ]] ; then
InMenuEntry=false
if [[ $InSubMenu == true ]] ; then
NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
else
NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
fi
elif [[ $InSubMenu == true ]] ; then
InSubMenu=false
NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
else
continue # Future error message?
fi
# Set maximum CurrText size to 68 characters.
CurrText="${CurrText:0:67}"
AllMenusArr+=($CurrTag "$CurrText")
fi

# Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
# "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
if [[ $line == submenu* ]] ; then
# line starts with `submenu`
InSubMenu=true
ThisSubMenuMajorNo=$NextMenuEntryNo
NextSubMenuMinorNo=0
SubMenuList=$SubMenuList$ThisSubMenuMajorNo
CurrTag=$NextMenuEntryNo
CurrText="${line#*'}"
CurrText="${CurrText%%'*}"
AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
# line starts with `menuentry` and ends with `{`
if [[ $HideUpstartRecovery == true ]] ; then
if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
SkippedMenuEntry=true
continue
fi
fi
InMenuEntry=true
if [[ $InSubMenu == true ]] ; then
: # In a submenu, increment minor instead of major which is "sticky" now.
CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
else
CurrTag=$NextMenuEntryNo
fi
CurrText="${line#*'}"
CurrText="${CurrText%%'*}"

else
continue # Other stuff - Ignore it.
fi

done < /boot/grub/grub.cfg

LongVersion=$(grub-install --version)
ShortVersion=$(echo "${LongVersion:20}")
DefaultItem=0

if [[ $HideUpstartRecovery == true ]] ; then
MenuText="Menu No. ----------- Menu Name -----------"
else
MenuText="Menu No. --------------- Menu Name ---------------"
fi

while true ; do

Choice=$(whiptail
--title "Use arrow, page, home & end keys. Tab toggle option"
--backtitle "Grub Version: $ShortVersion"
--ok-button "Display Grub Boot"
--cancel-button "Exit"
--default-item "$DefaultItem"
--menu "$MenuText" 24 76 16
"${AllMenusArr[@]}"
2>&1 >/dev/tty)

clear
if [[ $Choice == "" ]]; then break ; fi
DefaultItem=$Choice

for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
i=$i+1
MenuEntry="menuentry '"${AllMenusArr[i]}"'"
break
fi
done

TheGameIsAfoot=false
while read -r line ; do
if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
if [[ $TheGameIsAfoot == true ]]; then
echo $line
if [[ $line = *"}"* ]]; then break ; fi
fi
done < /boot/grub/grub.cfg

read -p "Press <Enter> to continue"

done

exit 0








share|improve this answer

































    3














    This should be the right "string"...



    awk -F' '/^menuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


    The last pipe delete white space at the beginning of each row and correct the number sequence in accordance with grub number entry.
    Sample output:



    0   Ubuntu
    1 Memory test (memtest86+)
    2 Memory test (memtest86+, serial console 115200)
    3 Windows 10 (su /dev/sdc1)


    If you like to see all entries, also the advanced, you could use



    awk -F' '/(^|| )nuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


    but the numbers sequence isn't suitable for use with grub-set-default.






    share|improve this answer





















    • 6





      This only shows the top level menu. The Advanced Options sub-menu is missing.

      – WinEunuuchs2Unix
      Jan 24 '18 at 23:52






    • 1





      Could you explain what the piped commands do and how it is better than the accepted answer?

      – Melebius
      Jan 25 '18 at 10:00











    • For me, this worked better as it prints also the entry numbers

      – Zac
      Jan 29 '18 at 16:08



















    2














    I just found out that you can just use auto-complete (pressing tab twice) on commands like grub-reboot or grub-set-default and it'll give you a list of the available boot menu entries. You can then just copy the title you want literally, you don't need to use a number.



    Unfortunately, for some reason my Windows 7 installation doesn't show up that way.






    share|improve this answer

































      0














      In OpenSuSE grub2-once provides the menu id which you can then use in other commands. Not much help for Ubuntu users though.



      # grub2-once --list
      0 openSUSE Leap 42.3
      1 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default
      2 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default (recovery mode)
      3 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default
      4 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default (recovery mode)
      5 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default
      6 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default (recovery mode)
      7 openSUSE 42.1 (x86_64) (on /dev/sda8)
      8 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1 (on /dev/sda8)
      9 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-56-default (on /dev/sda8)
      10 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-53-default (on /dev/sda8)
      11 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>Install 42.3 (on /dev/sda8)





      share|improve this answer


























      • You should probably share the location that you got grub2-once from

        – Charles Green
        Feb 11 at 16:38











      • Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

        – Paranoid
        Feb 11 at 17:35











      • Seems like a handy utility...

        – Charles Green
        Feb 11 at 17:38



















      0














      An other awk one-liner which handles submenus :



       awk -F' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /tmenuentry / {print "t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg


      This might be fallible thought, because t is used to identify submenu entries.






      share|improve this answer























        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "89"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f599208%2fhow-to-list-grubs-menuentries-in-command-line%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        29














        Using awk



        awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg 


        gives you a full list of all menu entries in grub.cfg.






        share|improve this answer





















        • 1





          You do not need to pipe grep to awk , awk will pattern match with /foo/

          – Panther
          Mar 20 '15 at 17:06






        • 1





          awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

          – Panther
          Mar 20 '15 at 17:57











        • A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

          – Fabby
          Aug 9 '15 at 20:40











        • @Fabby a note for you. OMG Those were my first steps with awk. :

          – A.B.
          Aug 9 '15 at 21:03






        • 3





          Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

          – mikezter
          Apr 28 '17 at 18:42
















        29














        Using awk



        awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg 


        gives you a full list of all menu entries in grub.cfg.






        share|improve this answer





















        • 1





          You do not need to pipe grep to awk , awk will pattern match with /foo/

          – Panther
          Mar 20 '15 at 17:06






        • 1





          awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

          – Panther
          Mar 20 '15 at 17:57











        • A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

          – Fabby
          Aug 9 '15 at 20:40











        • @Fabby a note for you. OMG Those were my first steps with awk. :

          – A.B.
          Aug 9 '15 at 21:03






        • 3





          Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

          – mikezter
          Apr 28 '17 at 18:42














        29












        29








        29







        Using awk



        awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg 


        gives you a full list of all menu entries in grub.cfg.






        share|improve this answer















        Using awk



        awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg 


        gives you a full list of all menu entries in grub.cfg.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 9 '15 at 21:01

























        answered Mar 20 '15 at 17:02









        A.B.A.B.

        69.5k12172266




        69.5k12172266








        • 1





          You do not need to pipe grep to awk , awk will pattern match with /foo/

          – Panther
          Mar 20 '15 at 17:06






        • 1





          awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

          – Panther
          Mar 20 '15 at 17:57











        • A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

          – Fabby
          Aug 9 '15 at 20:40











        • @Fabby a note for you. OMG Those were my first steps with awk. :

          – A.B.
          Aug 9 '15 at 21:03






        • 3





          Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

          – mikezter
          Apr 28 '17 at 18:42














        • 1





          You do not need to pipe grep to awk , awk will pattern match with /foo/

          – Panther
          Mar 20 '15 at 17:06






        • 1





          awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

          – Panther
          Mar 20 '15 at 17:57











        • A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

          – Fabby
          Aug 9 '15 at 20:40











        • @Fabby a note for you. OMG Those were my first steps with awk. :

          – A.B.
          Aug 9 '15 at 21:03






        • 3





          Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

          – mikezter
          Apr 28 '17 at 18:42








        1




        1





        You do not need to pipe grep to awk , awk will pattern match with /foo/

        – Panther
        Mar 20 '15 at 17:06





        You do not need to pipe grep to awk , awk will pattern match with /foo/

        – Panther
        Mar 20 '15 at 17:06




        1




        1





        awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

        – Panther
        Mar 20 '15 at 17:57





        awk -F' '/menuentry / {print $2}' /boot/grub/grub.cfg

        – Panther
        Mar 20 '15 at 17:57













        A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

        – Fabby
        Aug 9 '15 at 20:40





        A.B. I agree with @bodhi.zazen. If you edit and leave me a note, I'mm come back and upvote!

        – Fabby
        Aug 9 '15 at 20:40













        @Fabby a note for you. OMG Those were my first steps with awk. :

        – A.B.
        Aug 9 '15 at 21:03





        @Fabby a note for you. OMG Those were my first steps with awk. :

        – A.B.
        Aug 9 '15 at 21:03




        3




        3





        Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

        – mikezter
        Apr 28 '17 at 18:42





        Add the entry number to the output: awk -F' '/menuentry / { print i++, $2}' /boot/grub/grub.cfg to use for grub-set-default

        – mikezter
        Apr 28 '17 at 18:42













        7














        Exactly this has been annoying me for over a year - so I did a quick and dirty script to do it. Hope this helps others?



        This stacks the names in a push/pop queue, but not the menu indexing so it could be better but I have gone as far as I could be bothered.



        gawk  'BEGIN {                                                                                                                       
        l=0
        menuindex= 0
        stack[t=0] = 0
        }

        function push(x) { stack[t++] = x }

        function pop() { if (t > 0) { return stack[--t] } else { return "" } }

        {

        if( $0 ~ /.*menu.*{.*/ )
        {
        push( $0 )
        l++;

        } else if( $0 ~ /.*{.*/ )
        {
        push( $0 )

        } else if( $0 ~ /.*}.*/ )
        {
        X = pop()
        if( X ~ /.*menu.*{.*/ )
        {
        l--;
        match( X, /^[^''']*'''([^''']*)'''.*$/, arr )

        if( l == 0 )
        {
        print menuindex ": " arr[1]
        menuindex++
        submenu=0
        } else
        {
        print " " (menuindex-1) ">" submenu " " arr[1]
        submenu++
        }
        }
        }

        }' /boot/grub/grub.cfg


        Here you can see a screen grab from my box showing it running
        enter image description here






        share|improve this answer


























        • +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

          – WinEunuuchs2Unix
          Apr 6 '18 at 23:55


















        7














        Exactly this has been annoying me for over a year - so I did a quick and dirty script to do it. Hope this helps others?



        This stacks the names in a push/pop queue, but not the menu indexing so it could be better but I have gone as far as I could be bothered.



        gawk  'BEGIN {                                                                                                                       
        l=0
        menuindex= 0
        stack[t=0] = 0
        }

        function push(x) { stack[t++] = x }

        function pop() { if (t > 0) { return stack[--t] } else { return "" } }

        {

        if( $0 ~ /.*menu.*{.*/ )
        {
        push( $0 )
        l++;

        } else if( $0 ~ /.*{.*/ )
        {
        push( $0 )

        } else if( $0 ~ /.*}.*/ )
        {
        X = pop()
        if( X ~ /.*menu.*{.*/ )
        {
        l--;
        match( X, /^[^''']*'''([^''']*)'''.*$/, arr )

        if( l == 0 )
        {
        print menuindex ": " arr[1]
        menuindex++
        submenu=0
        } else
        {
        print " " (menuindex-1) ">" submenu " " arr[1]
        submenu++
        }
        }
        }

        }' /boot/grub/grub.cfg


        Here you can see a screen grab from my box showing it running
        enter image description here






        share|improve this answer


























        • +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

          – WinEunuuchs2Unix
          Apr 6 '18 at 23:55
















        7












        7








        7







        Exactly this has been annoying me for over a year - so I did a quick and dirty script to do it. Hope this helps others?



        This stacks the names in a push/pop queue, but not the menu indexing so it could be better but I have gone as far as I could be bothered.



        gawk  'BEGIN {                                                                                                                       
        l=0
        menuindex= 0
        stack[t=0] = 0
        }

        function push(x) { stack[t++] = x }

        function pop() { if (t > 0) { return stack[--t] } else { return "" } }

        {

        if( $0 ~ /.*menu.*{.*/ )
        {
        push( $0 )
        l++;

        } else if( $0 ~ /.*{.*/ )
        {
        push( $0 )

        } else if( $0 ~ /.*}.*/ )
        {
        X = pop()
        if( X ~ /.*menu.*{.*/ )
        {
        l--;
        match( X, /^[^''']*'''([^''']*)'''.*$/, arr )

        if( l == 0 )
        {
        print menuindex ": " arr[1]
        menuindex++
        submenu=0
        } else
        {
        print " " (menuindex-1) ">" submenu " " arr[1]
        submenu++
        }
        }
        }

        }' /boot/grub/grub.cfg


        Here you can see a screen grab from my box showing it running
        enter image description here






        share|improve this answer















        Exactly this has been annoying me for over a year - so I did a quick and dirty script to do it. Hope this helps others?



        This stacks the names in a push/pop queue, but not the menu indexing so it could be better but I have gone as far as I could be bothered.



        gawk  'BEGIN {                                                                                                                       
        l=0
        menuindex= 0
        stack[t=0] = 0
        }

        function push(x) { stack[t++] = x }

        function pop() { if (t > 0) { return stack[--t] } else { return "" } }

        {

        if( $0 ~ /.*menu.*{.*/ )
        {
        push( $0 )
        l++;

        } else if( $0 ~ /.*{.*/ )
        {
        push( $0 )

        } else if( $0 ~ /.*}.*/ )
        {
        X = pop()
        if( X ~ /.*menu.*{.*/ )
        {
        l--;
        match( X, /^[^''']*'''([^''']*)'''.*$/, arr )

        if( l == 0 )
        {
        print menuindex ": " arr[1]
        menuindex++
        submenu=0
        } else
        {
        print " " (menuindex-1) ">" submenu " " arr[1]
        submenu++
        }
        }
        }

        }' /boot/grub/grub.cfg


        Here you can see a screen grab from my box showing it running
        enter image description here







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 1 '17 at 18:23

























        answered Aug 1 '17 at 17:54









        Lucien Murray-PittsLucien Murray-Pitts

        7113




        7113













        • +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

          – WinEunuuchs2Unix
          Apr 6 '18 at 23:55





















        • +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

          – WinEunuuchs2Unix
          Apr 6 '18 at 23:55



















        +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

        – WinEunuuchs2Unix
        Apr 6 '18 at 23:55







        +1 I like the push and pop commands in your answer. Wish I had known them before I wrote my answer.

        – WinEunuuchs2Unix
        Apr 6 '18 at 23:55













        7














        Color Screen shot (short version)



        Use mouse scroll wheel, Home, End, PgUp, PgDn, and keys to navigate the menu.



        grub-menu.sh



        Text Screen shot (long version)



        The bash script uses whiptail instead of dialog to display the menu. One advantage is you can copy the terminal image to the clipboard as
        text and paste it into this website as text. Other advantages include:




        • Mouse scroll wheel support

        • Faster performance


        • dialog is not installed by default in Ubuntu Server or Lubuntu. whiptail is included by default.


        Here's a text screen shot:



        Grub Version: 2.02~beta2-36ubuntu3.15


        ┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
        │ Menu No. --------------- Menu Name --------------- │
        │ │
        │ 0 Ubuntu ↑ │
        │ 1 Advanced options for Ubuntu ▮ │
        │ 1>0 Ubuntu, with Linux 4.14.31-041431-generic ▒ │
        │ 1>1 Ubuntu, with Linux 4.14.31-041431-generic (upstart) ▒ │
        │ 1>2 Ubuntu, with Linux 4.14.31-041431-generic (recovery mode) ▒ │
        │ 1>3 Ubuntu, with Linux 4.14.30-041430-generic ▒ │
        │ 1>4 Ubuntu, with Linux 4.14.30-041430-generic (upstart) ▒ │
        │ 1>5 Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) ▒ │
        │ 1>6 Ubuntu, with Linux 4.14.27-041427-generic ▒ │
        │ 1>7 Ubuntu, with Linux 4.14.27-041427-generic (upstart) ▒ │
        │ 1>8 Ubuntu, with Linux 4.14.27-041427-generic (recovery mode) ▒ │
        │ 1>9 Ubuntu, with Linux 4.14.24-041424-generic ▒ │
        │ 1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart) ▒ │
        │ 1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode) ▒ │
        │ 1>12 Ubuntu, with Linux 4.14.23-041423-generic ▒ │
        │ 1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart) ↓ │
        │ │
        │ │
        │ <Display Grub Boot> <Exit> │
        │ │
        └──────────────────────────────────────────────────────────────────────────┘


        Highlight entry and press Enter



        Use the navigation keys to highlight an option and press Enter to see the pre-kernel drivers loaded by grub and the boot parameters passed by grub to the kernel when booting it:



        menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
        recordfail
        savedefault
        load_video
        gfxmode $linux_gfx_mode
        insmod gzio
        if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
        insmod part_gpt
        insmod ext2
        if [ x$feature_platform_search_hint = xy ]; then
        search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
        else
        search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
        fi
        echo 'Loading Linux 4.14.27-041427-generic ...'
        linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
        echo 'Loading initial ramdisk ...'
        initrd /boot/initrd.img-4.14.27-041427-generic
        }
        Press <Enter> to continue





        grub-menu.sh bash script





        grub-menu.sh only has one option to tweak:



        # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
        HideUpstartRecovery=false


        Set the value to true (hide the extra entries) or false (list all entries).



        The default format can be overridden when calling the script using:



        grub-menu.sh short


        or:



        grub-menu.sh long


        The code:



        #!/bin/bash

        # NAME: grub-menu.sh
        # PATH: $HOME/bin
        # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
        # DATE: Apr 5, 2018. Modified: May 7, 2018.

        # $TERM variable may be missing when called via desktop shortcut
        CurrentTERM=$(env | grep TERM)
        if [[ $CurrentTERM == "" ]] ; then
        notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
        exit 1
        fi

        AllMenusArr=() # All menu options.
        # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
        HideUpstartRecovery=false
        if [[ $1 == short ]] ; then
        HideUpstartRecovery=true # override default with first passed parameter "short"
        elif [[ $1 == long ]] ; then
        HideUpstartRecovery=false # override default with first passed parameter "long"
        fi
        SkippedMenuEntry=false # Don't change this value, automatically maintained
        InSubMenu=false # Within a line beginning with `submenu`?
        InMenuEntry=false # Within a line beginning with `menuentry` and ending in `{`?
        NextMenuEntryNo=0 # Next grub internal menu entry number to assign
        # Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
        ThisSubMenuMajorNo=0
        NextSubMenuMinorNo=0
        CurrTag="" # Current grub internal menu number, zero based
        CurrText="" # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
        SubMenuList="" # Only supports 10 submenus! Numbered 0 to 9. Future use.

        while read -r line; do
        # Example: " }"
        BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
        if [[ $BlackLine == "}" ]] ; then
        # Add menu option in buffer
        if [[ $SkippedMenuEntry == true ]] ; then
        NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
        SkippedMenuEntry=false
        continue
        fi
        if [[ $InMenuEntry == true ]] ; then
        InMenuEntry=false
        if [[ $InSubMenu == true ]] ; then
        NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
        else
        NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
        fi
        elif [[ $InSubMenu == true ]] ; then
        InSubMenu=false
        NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
        else
        continue # Future error message?
        fi
        # Set maximum CurrText size to 68 characters.
        CurrText="${CurrText:0:67}"
        AllMenusArr+=($CurrTag "$CurrText")
        fi

        # Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
        # "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
        if [[ $line == submenu* ]] ; then
        # line starts with `submenu`
        InSubMenu=true
        ThisSubMenuMajorNo=$NextMenuEntryNo
        NextSubMenuMinorNo=0
        SubMenuList=$SubMenuList$ThisSubMenuMajorNo
        CurrTag=$NextMenuEntryNo
        CurrText="${line#*'}"
        CurrText="${CurrText%%'*}"
        AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

        elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
        # line starts with `menuentry` and ends with `{`
        if [[ $HideUpstartRecovery == true ]] ; then
        if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
        SkippedMenuEntry=true
        continue
        fi
        fi
        InMenuEntry=true
        if [[ $InSubMenu == true ]] ; then
        : # In a submenu, increment minor instead of major which is "sticky" now.
        CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
        else
        CurrTag=$NextMenuEntryNo
        fi
        CurrText="${line#*'}"
        CurrText="${CurrText%%'*}"

        else
        continue # Other stuff - Ignore it.
        fi

        done < /boot/grub/grub.cfg

        LongVersion=$(grub-install --version)
        ShortVersion=$(echo "${LongVersion:20}")
        DefaultItem=0

        if [[ $HideUpstartRecovery == true ]] ; then
        MenuText="Menu No. ----------- Menu Name -----------"
        else
        MenuText="Menu No. --------------- Menu Name ---------------"
        fi

        while true ; do

        Choice=$(whiptail
        --title "Use arrow, page, home & end keys. Tab toggle option"
        --backtitle "Grub Version: $ShortVersion"
        --ok-button "Display Grub Boot"
        --cancel-button "Exit"
        --default-item "$DefaultItem"
        --menu "$MenuText" 24 76 16
        "${AllMenusArr[@]}"
        2>&1 >/dev/tty)

        clear
        if [[ $Choice == "" ]]; then break ; fi
        DefaultItem=$Choice

        for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
        if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
        i=$i+1
        MenuEntry="menuentry '"${AllMenusArr[i]}"'"
        break
        fi
        done

        TheGameIsAfoot=false
        while read -r line ; do
        if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
        if [[ $TheGameIsAfoot == true ]]; then
        echo $line
        if [[ $line = *"}"* ]]; then break ; fi
        fi
        done < /boot/grub/grub.cfg

        read -p "Press <Enter> to continue"

        done

        exit 0








        share|improve this answer






























          7














          Color Screen shot (short version)



          Use mouse scroll wheel, Home, End, PgUp, PgDn, and keys to navigate the menu.



          grub-menu.sh



          Text Screen shot (long version)



          The bash script uses whiptail instead of dialog to display the menu. One advantage is you can copy the terminal image to the clipboard as
          text and paste it into this website as text. Other advantages include:




          • Mouse scroll wheel support

          • Faster performance


          • dialog is not installed by default in Ubuntu Server or Lubuntu. whiptail is included by default.


          Here's a text screen shot:



          Grub Version: 2.02~beta2-36ubuntu3.15


          ┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
          │ Menu No. --------------- Menu Name --------------- │
          │ │
          │ 0 Ubuntu ↑ │
          │ 1 Advanced options for Ubuntu ▮ │
          │ 1>0 Ubuntu, with Linux 4.14.31-041431-generic ▒ │
          │ 1>1 Ubuntu, with Linux 4.14.31-041431-generic (upstart) ▒ │
          │ 1>2 Ubuntu, with Linux 4.14.31-041431-generic (recovery mode) ▒ │
          │ 1>3 Ubuntu, with Linux 4.14.30-041430-generic ▒ │
          │ 1>4 Ubuntu, with Linux 4.14.30-041430-generic (upstart) ▒ │
          │ 1>5 Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) ▒ │
          │ 1>6 Ubuntu, with Linux 4.14.27-041427-generic ▒ │
          │ 1>7 Ubuntu, with Linux 4.14.27-041427-generic (upstart) ▒ │
          │ 1>8 Ubuntu, with Linux 4.14.27-041427-generic (recovery mode) ▒ │
          │ 1>9 Ubuntu, with Linux 4.14.24-041424-generic ▒ │
          │ 1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart) ▒ │
          │ 1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode) ▒ │
          │ 1>12 Ubuntu, with Linux 4.14.23-041423-generic ▒ │
          │ 1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart) ↓ │
          │ │
          │ │
          │ <Display Grub Boot> <Exit> │
          │ │
          └──────────────────────────────────────────────────────────────────────────┘


          Highlight entry and press Enter



          Use the navigation keys to highlight an option and press Enter to see the pre-kernel drivers loaded by grub and the boot parameters passed by grub to the kernel when booting it:



          menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
          recordfail
          savedefault
          load_video
          gfxmode $linux_gfx_mode
          insmod gzio
          if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
          insmod part_gpt
          insmod ext2
          if [ x$feature_platform_search_hint = xy ]; then
          search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
          else
          search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
          fi
          echo 'Loading Linux 4.14.27-041427-generic ...'
          linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
          echo 'Loading initial ramdisk ...'
          initrd /boot/initrd.img-4.14.27-041427-generic
          }
          Press <Enter> to continue





          grub-menu.sh bash script





          grub-menu.sh only has one option to tweak:



          # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
          HideUpstartRecovery=false


          Set the value to true (hide the extra entries) or false (list all entries).



          The default format can be overridden when calling the script using:



          grub-menu.sh short


          or:



          grub-menu.sh long


          The code:



          #!/bin/bash

          # NAME: grub-menu.sh
          # PATH: $HOME/bin
          # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
          # DATE: Apr 5, 2018. Modified: May 7, 2018.

          # $TERM variable may be missing when called via desktop shortcut
          CurrentTERM=$(env | grep TERM)
          if [[ $CurrentTERM == "" ]] ; then
          notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
          exit 1
          fi

          AllMenusArr=() # All menu options.
          # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
          HideUpstartRecovery=false
          if [[ $1 == short ]] ; then
          HideUpstartRecovery=true # override default with first passed parameter "short"
          elif [[ $1 == long ]] ; then
          HideUpstartRecovery=false # override default with first passed parameter "long"
          fi
          SkippedMenuEntry=false # Don't change this value, automatically maintained
          InSubMenu=false # Within a line beginning with `submenu`?
          InMenuEntry=false # Within a line beginning with `menuentry` and ending in `{`?
          NextMenuEntryNo=0 # Next grub internal menu entry number to assign
          # Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
          ThisSubMenuMajorNo=0
          NextSubMenuMinorNo=0
          CurrTag="" # Current grub internal menu number, zero based
          CurrText="" # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
          SubMenuList="" # Only supports 10 submenus! Numbered 0 to 9. Future use.

          while read -r line; do
          # Example: " }"
          BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
          if [[ $BlackLine == "}" ]] ; then
          # Add menu option in buffer
          if [[ $SkippedMenuEntry == true ]] ; then
          NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
          SkippedMenuEntry=false
          continue
          fi
          if [[ $InMenuEntry == true ]] ; then
          InMenuEntry=false
          if [[ $InSubMenu == true ]] ; then
          NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
          else
          NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
          fi
          elif [[ $InSubMenu == true ]] ; then
          InSubMenu=false
          NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
          else
          continue # Future error message?
          fi
          # Set maximum CurrText size to 68 characters.
          CurrText="${CurrText:0:67}"
          AllMenusArr+=($CurrTag "$CurrText")
          fi

          # Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
          # "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
          if [[ $line == submenu* ]] ; then
          # line starts with `submenu`
          InSubMenu=true
          ThisSubMenuMajorNo=$NextMenuEntryNo
          NextSubMenuMinorNo=0
          SubMenuList=$SubMenuList$ThisSubMenuMajorNo
          CurrTag=$NextMenuEntryNo
          CurrText="${line#*'}"
          CurrText="${CurrText%%'*}"
          AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

          elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
          # line starts with `menuentry` and ends with `{`
          if [[ $HideUpstartRecovery == true ]] ; then
          if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
          SkippedMenuEntry=true
          continue
          fi
          fi
          InMenuEntry=true
          if [[ $InSubMenu == true ]] ; then
          : # In a submenu, increment minor instead of major which is "sticky" now.
          CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
          else
          CurrTag=$NextMenuEntryNo
          fi
          CurrText="${line#*'}"
          CurrText="${CurrText%%'*}"

          else
          continue # Other stuff - Ignore it.
          fi

          done < /boot/grub/grub.cfg

          LongVersion=$(grub-install --version)
          ShortVersion=$(echo "${LongVersion:20}")
          DefaultItem=0

          if [[ $HideUpstartRecovery == true ]] ; then
          MenuText="Menu No. ----------- Menu Name -----------"
          else
          MenuText="Menu No. --------------- Menu Name ---------------"
          fi

          while true ; do

          Choice=$(whiptail
          --title "Use arrow, page, home & end keys. Tab toggle option"
          --backtitle "Grub Version: $ShortVersion"
          --ok-button "Display Grub Boot"
          --cancel-button "Exit"
          --default-item "$DefaultItem"
          --menu "$MenuText" 24 76 16
          "${AllMenusArr[@]}"
          2>&1 >/dev/tty)

          clear
          if [[ $Choice == "" ]]; then break ; fi
          DefaultItem=$Choice

          for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
          if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
          i=$i+1
          MenuEntry="menuentry '"${AllMenusArr[i]}"'"
          break
          fi
          done

          TheGameIsAfoot=false
          while read -r line ; do
          if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
          if [[ $TheGameIsAfoot == true ]]; then
          echo $line
          if [[ $line = *"}"* ]]; then break ; fi
          fi
          done < /boot/grub/grub.cfg

          read -p "Press <Enter> to continue"

          done

          exit 0








          share|improve this answer




























            7












            7








            7







            Color Screen shot (short version)



            Use mouse scroll wheel, Home, End, PgUp, PgDn, and keys to navigate the menu.



            grub-menu.sh



            Text Screen shot (long version)



            The bash script uses whiptail instead of dialog to display the menu. One advantage is you can copy the terminal image to the clipboard as
            text and paste it into this website as text. Other advantages include:




            • Mouse scroll wheel support

            • Faster performance


            • dialog is not installed by default in Ubuntu Server or Lubuntu. whiptail is included by default.


            Here's a text screen shot:



            Grub Version: 2.02~beta2-36ubuntu3.15


            ┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
            │ Menu No. --------------- Menu Name --------------- │
            │ │
            │ 0 Ubuntu ↑ │
            │ 1 Advanced options for Ubuntu ▮ │
            │ 1>0 Ubuntu, with Linux 4.14.31-041431-generic ▒ │
            │ 1>1 Ubuntu, with Linux 4.14.31-041431-generic (upstart) ▒ │
            │ 1>2 Ubuntu, with Linux 4.14.31-041431-generic (recovery mode) ▒ │
            │ 1>3 Ubuntu, with Linux 4.14.30-041430-generic ▒ │
            │ 1>4 Ubuntu, with Linux 4.14.30-041430-generic (upstart) ▒ │
            │ 1>5 Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) ▒ │
            │ 1>6 Ubuntu, with Linux 4.14.27-041427-generic ▒ │
            │ 1>7 Ubuntu, with Linux 4.14.27-041427-generic (upstart) ▒ │
            │ 1>8 Ubuntu, with Linux 4.14.27-041427-generic (recovery mode) ▒ │
            │ 1>9 Ubuntu, with Linux 4.14.24-041424-generic ▒ │
            │ 1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart) ▒ │
            │ 1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode) ▒ │
            │ 1>12 Ubuntu, with Linux 4.14.23-041423-generic ▒ │
            │ 1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart) ↓ │
            │ │
            │ │
            │ <Display Grub Boot> <Exit> │
            │ │
            └──────────────────────────────────────────────────────────────────────────┘


            Highlight entry and press Enter



            Use the navigation keys to highlight an option and press Enter to see the pre-kernel drivers loaded by grub and the boot parameters passed by grub to the kernel when booting it:



            menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
            recordfail
            savedefault
            load_video
            gfxmode $linux_gfx_mode
            insmod gzio
            if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
            insmod part_gpt
            insmod ext2
            if [ x$feature_platform_search_hint = xy ]; then
            search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
            else
            search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
            fi
            echo 'Loading Linux 4.14.27-041427-generic ...'
            linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
            echo 'Loading initial ramdisk ...'
            initrd /boot/initrd.img-4.14.27-041427-generic
            }
            Press <Enter> to continue





            grub-menu.sh bash script





            grub-menu.sh only has one option to tweak:



            # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
            HideUpstartRecovery=false


            Set the value to true (hide the extra entries) or false (list all entries).



            The default format can be overridden when calling the script using:



            grub-menu.sh short


            or:



            grub-menu.sh long


            The code:



            #!/bin/bash

            # NAME: grub-menu.sh
            # PATH: $HOME/bin
            # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
            # DATE: Apr 5, 2018. Modified: May 7, 2018.

            # $TERM variable may be missing when called via desktop shortcut
            CurrentTERM=$(env | grep TERM)
            if [[ $CurrentTERM == "" ]] ; then
            notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
            exit 1
            fi

            AllMenusArr=() # All menu options.
            # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
            HideUpstartRecovery=false
            if [[ $1 == short ]] ; then
            HideUpstartRecovery=true # override default with first passed parameter "short"
            elif [[ $1 == long ]] ; then
            HideUpstartRecovery=false # override default with first passed parameter "long"
            fi
            SkippedMenuEntry=false # Don't change this value, automatically maintained
            InSubMenu=false # Within a line beginning with `submenu`?
            InMenuEntry=false # Within a line beginning with `menuentry` and ending in `{`?
            NextMenuEntryNo=0 # Next grub internal menu entry number to assign
            # Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
            ThisSubMenuMajorNo=0
            NextSubMenuMinorNo=0
            CurrTag="" # Current grub internal menu number, zero based
            CurrText="" # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
            SubMenuList="" # Only supports 10 submenus! Numbered 0 to 9. Future use.

            while read -r line; do
            # Example: " }"
            BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
            if [[ $BlackLine == "}" ]] ; then
            # Add menu option in buffer
            if [[ $SkippedMenuEntry == true ]] ; then
            NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
            SkippedMenuEntry=false
            continue
            fi
            if [[ $InMenuEntry == true ]] ; then
            InMenuEntry=false
            if [[ $InSubMenu == true ]] ; then
            NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
            else
            NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
            fi
            elif [[ $InSubMenu == true ]] ; then
            InSubMenu=false
            NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
            else
            continue # Future error message?
            fi
            # Set maximum CurrText size to 68 characters.
            CurrText="${CurrText:0:67}"
            AllMenusArr+=($CurrTag "$CurrText")
            fi

            # Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
            # "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
            if [[ $line == submenu* ]] ; then
            # line starts with `submenu`
            InSubMenu=true
            ThisSubMenuMajorNo=$NextMenuEntryNo
            NextSubMenuMinorNo=0
            SubMenuList=$SubMenuList$ThisSubMenuMajorNo
            CurrTag=$NextMenuEntryNo
            CurrText="${line#*'}"
            CurrText="${CurrText%%'*}"
            AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

            elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
            # line starts with `menuentry` and ends with `{`
            if [[ $HideUpstartRecovery == true ]] ; then
            if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
            SkippedMenuEntry=true
            continue
            fi
            fi
            InMenuEntry=true
            if [[ $InSubMenu == true ]] ; then
            : # In a submenu, increment minor instead of major which is "sticky" now.
            CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
            else
            CurrTag=$NextMenuEntryNo
            fi
            CurrText="${line#*'}"
            CurrText="${CurrText%%'*}"

            else
            continue # Other stuff - Ignore it.
            fi

            done < /boot/grub/grub.cfg

            LongVersion=$(grub-install --version)
            ShortVersion=$(echo "${LongVersion:20}")
            DefaultItem=0

            if [[ $HideUpstartRecovery == true ]] ; then
            MenuText="Menu No. ----------- Menu Name -----------"
            else
            MenuText="Menu No. --------------- Menu Name ---------------"
            fi

            while true ; do

            Choice=$(whiptail
            --title "Use arrow, page, home & end keys. Tab toggle option"
            --backtitle "Grub Version: $ShortVersion"
            --ok-button "Display Grub Boot"
            --cancel-button "Exit"
            --default-item "$DefaultItem"
            --menu "$MenuText" 24 76 16
            "${AllMenusArr[@]}"
            2>&1 >/dev/tty)

            clear
            if [[ $Choice == "" ]]; then break ; fi
            DefaultItem=$Choice

            for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
            if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
            i=$i+1
            MenuEntry="menuentry '"${AllMenusArr[i]}"'"
            break
            fi
            done

            TheGameIsAfoot=false
            while read -r line ; do
            if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
            if [[ $TheGameIsAfoot == true ]]; then
            echo $line
            if [[ $line = *"}"* ]]; then break ; fi
            fi
            done < /boot/grub/grub.cfg

            read -p "Press <Enter> to continue"

            done

            exit 0








            share|improve this answer















            Color Screen shot (short version)



            Use mouse scroll wheel, Home, End, PgUp, PgDn, and keys to navigate the menu.



            grub-menu.sh



            Text Screen shot (long version)



            The bash script uses whiptail instead of dialog to display the menu. One advantage is you can copy the terminal image to the clipboard as
            text and paste it into this website as text. Other advantages include:




            • Mouse scroll wheel support

            • Faster performance


            • dialog is not installed by default in Ubuntu Server or Lubuntu. whiptail is included by default.


            Here's a text screen shot:



            Grub Version: 2.02~beta2-36ubuntu3.15


            ┌─────────┤ Use arrow, page, home & end keys. Tab toggle option ├──────────┐
            │ Menu No. --------------- Menu Name --------------- │
            │ │
            │ 0 Ubuntu ↑ │
            │ 1 Advanced options for Ubuntu ▮ │
            │ 1>0 Ubuntu, with Linux 4.14.31-041431-generic ▒ │
            │ 1>1 Ubuntu, with Linux 4.14.31-041431-generic (upstart) ▒ │
            │ 1>2 Ubuntu, with Linux 4.14.31-041431-generic (recovery mode) ▒ │
            │ 1>3 Ubuntu, with Linux 4.14.30-041430-generic ▒ │
            │ 1>4 Ubuntu, with Linux 4.14.30-041430-generic (upstart) ▒ │
            │ 1>5 Ubuntu, with Linux 4.14.30-041430-generic (recovery mode) ▒ │
            │ 1>6 Ubuntu, with Linux 4.14.27-041427-generic ▒ │
            │ 1>7 Ubuntu, with Linux 4.14.27-041427-generic (upstart) ▒ │
            │ 1>8 Ubuntu, with Linux 4.14.27-041427-generic (recovery mode) ▒ │
            │ 1>9 Ubuntu, with Linux 4.14.24-041424-generic ▒ │
            │ 1>10 Ubuntu, with Linux 4.14.24-041424-generic (upstart) ▒ │
            │ 1>11 Ubuntu, with Linux 4.14.24-041424-generic (recovery mode) ▒ │
            │ 1>12 Ubuntu, with Linux 4.14.23-041423-generic ▒ │
            │ 1>13 Ubuntu, with Linux 4.14.23-041423-generic (upstart) ↓ │
            │ │
            │ │
            │ <Display Grub Boot> <Exit> │
            │ │
            └──────────────────────────────────────────────────────────────────────────┘


            Highlight entry and press Enter



            Use the navigation keys to highlight an option and press Enter to see the pre-kernel drivers loaded by grub and the boot parameters passed by grub to the kernel when booting it:



            menuentry 'Ubuntu, with Linux 4.14.27-041427-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-4.14.27-041427-generic-advanced-f3f8e7bc-b337-4194-88b8-3a513f6be55b' {
            recordfail
            savedefault
            load_video
            gfxmode $linux_gfx_mode
            insmod gzio
            if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
            insmod part_gpt
            insmod ext2
            if [ x$feature_platform_search_hint = xy ]; then
            search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
            else
            search --no-floppy --fs-uuid --set=root f3f8e7bc-b337-4194-88b8-3a513f6be55b
            fi
            echo 'Loading Linux 4.14.27-041427-generic ...'
            linux /boot/vmlinuz-4.14.27-041427-generic root=UUID=f3f8e7bc-b337-4194-88b8-3a513f6be55b ro quiet splash loglevel=0 vga=current udev.log-priority=3 fastboot kaslr acpiphp.disable=1 crashkernel=384M-2G:128M,2G-:256M $vt_handoff
            echo 'Loading initial ramdisk ...'
            initrd /boot/initrd.img-4.14.27-041427-generic
            }
            Press <Enter> to continue





            grub-menu.sh bash script





            grub-menu.sh only has one option to tweak:



            # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
            HideUpstartRecovery=false


            Set the value to true (hide the extra entries) or false (list all entries).



            The default format can be overridden when calling the script using:



            grub-menu.sh short


            or:



            grub-menu.sh long


            The code:



            #!/bin/bash

            # NAME: grub-menu.sh
            # PATH: $HOME/bin
            # DESC: Written for AU Q&A: https://askubuntu.com/q/1019213/307523
            # DATE: Apr 5, 2018. Modified: May 7, 2018.

            # $TERM variable may be missing when called via desktop shortcut
            CurrentTERM=$(env | grep TERM)
            if [[ $CurrentTERM == "" ]] ; then
            notify-send --urgency=critical "$0 cannot be run from GUI without TERM environment variable."
            exit 1
            fi

            AllMenusArr=() # All menu options.
            # Default for hide duplicate and triplicate options with (upstart) and (recovery mode)?
            HideUpstartRecovery=false
            if [[ $1 == short ]] ; then
            HideUpstartRecovery=true # override default with first passed parameter "short"
            elif [[ $1 == long ]] ; then
            HideUpstartRecovery=false # override default with first passed parameter "long"
            fi
            SkippedMenuEntry=false # Don't change this value, automatically maintained
            InSubMenu=false # Within a line beginning with `submenu`?
            InMenuEntry=false # Within a line beginning with `menuentry` and ending in `{`?
            NextMenuEntryNo=0 # Next grub internal menu entry number to assign
            # Major / Minor internal grub submenu numbers, ie `1>0`, `1>1`, `1>2`, etc.
            ThisSubMenuMajorNo=0
            NextSubMenuMinorNo=0
            CurrTag="" # Current grub internal menu number, zero based
            CurrText="" # Current grub menu option text, ie "Ubuntu", "Windows...", etc.
            SubMenuList="" # Only supports 10 submenus! Numbered 0 to 9. Future use.

            while read -r line; do
            # Example: " }"
            BlackLine="${line//[[:blank:]]/}" # Remove all whitespace
            if [[ $BlackLine == "}" ]] ; then
            # Add menu option in buffer
            if [[ $SkippedMenuEntry == true ]] ; then
            NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
            SkippedMenuEntry=false
            continue
            fi
            if [[ $InMenuEntry == true ]] ; then
            InMenuEntry=false
            if [[ $InSubMenu == true ]] ; then
            NextSubMenuMinorNo=$(( $NextSubMenuMinorNo + 1 ))
            else
            NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
            fi
            elif [[ $InSubMenu == true ]] ; then
            InSubMenu=false
            NextMenuEntryNo=$(( $NextMenuEntryNo + 1 ))
            else
            continue # Future error message?
            fi
            # Set maximum CurrText size to 68 characters.
            CurrText="${CurrText:0:67}"
            AllMenusArr+=($CurrTag "$CurrText")
            fi

            # Example: "menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu" ...
            # "submenu 'Advanced options for Ubuntu' $menuentry_id_option" ...
            if [[ $line == submenu* ]] ; then
            # line starts with `submenu`
            InSubMenu=true
            ThisSubMenuMajorNo=$NextMenuEntryNo
            NextSubMenuMinorNo=0
            SubMenuList=$SubMenuList$ThisSubMenuMajorNo
            CurrTag=$NextMenuEntryNo
            CurrText="${line#*'}"
            CurrText="${CurrText%%'*}"
            AllMenusArr+=($CurrTag "$CurrText") # ie "1 Advanced options for Ubuntu"

            elif [[ $line == menuentry* ]] && [[ $line == *"{"* ]] ; then
            # line starts with `menuentry` and ends with `{`
            if [[ $HideUpstartRecovery == true ]] ; then
            if [[ $line == *"(upstart)"* ]] || [[ $line == *"(recovery mode)"* ]] ; then
            SkippedMenuEntry=true
            continue
            fi
            fi
            InMenuEntry=true
            if [[ $InSubMenu == true ]] ; then
            : # In a submenu, increment minor instead of major which is "sticky" now.
            CurrTag=$ThisSubMenuMajorNo">"$NextSubMenuMinorNo
            else
            CurrTag=$NextMenuEntryNo
            fi
            CurrText="${line#*'}"
            CurrText="${CurrText%%'*}"

            else
            continue # Other stuff - Ignore it.
            fi

            done < /boot/grub/grub.cfg

            LongVersion=$(grub-install --version)
            ShortVersion=$(echo "${LongVersion:20}")
            DefaultItem=0

            if [[ $HideUpstartRecovery == true ]] ; then
            MenuText="Menu No. ----------- Menu Name -----------"
            else
            MenuText="Menu No. --------------- Menu Name ---------------"
            fi

            while true ; do

            Choice=$(whiptail
            --title "Use arrow, page, home & end keys. Tab toggle option"
            --backtitle "Grub Version: $ShortVersion"
            --ok-button "Display Grub Boot"
            --cancel-button "Exit"
            --default-item "$DefaultItem"
            --menu "$MenuText" 24 76 16
            "${AllMenusArr[@]}"
            2>&1 >/dev/tty)

            clear
            if [[ $Choice == "" ]]; then break ; fi
            DefaultItem=$Choice

            for (( i=0; i < ${#AllMenusArr[@]}; i=i+2 )) ; do
            if [[ "${AllMenusArr[i]}" == $Choice ]] ; then
            i=$i+1
            MenuEntry="menuentry '"${AllMenusArr[i]}"'"
            break
            fi
            done

            TheGameIsAfoot=false
            while read -r line ; do
            if [[ $line = *"$MenuEntry"* ]]; then TheGameIsAfoot=true ; fi
            if [[ $TheGameIsAfoot == true ]]; then
            echo $line
            if [[ $line = *"}"* ]]; then break ; fi
            fi
            done < /boot/grub/grub.cfg

            read -p "Press <Enter> to continue"

            done

            exit 0









            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 8 '18 at 0:30

























            answered Apr 7 '18 at 0:14









            WinEunuuchs2UnixWinEunuuchs2Unix

            46.6k1190182




            46.6k1190182























                3














                This should be the right "string"...



                awk -F' '/^menuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                The last pipe delete white space at the beginning of each row and correct the number sequence in accordance with grub number entry.
                Sample output:



                0   Ubuntu
                1 Memory test (memtest86+)
                2 Memory test (memtest86+, serial console 115200)
                3 Windows 10 (su /dev/sdc1)


                If you like to see all entries, also the advanced, you could use



                awk -F' '/(^|| )nuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                but the numbers sequence isn't suitable for use with grub-set-default.






                share|improve this answer





















                • 6





                  This only shows the top level menu. The Advanced Options sub-menu is missing.

                  – WinEunuuchs2Unix
                  Jan 24 '18 at 23:52






                • 1





                  Could you explain what the piped commands do and how it is better than the accepted answer?

                  – Melebius
                  Jan 25 '18 at 10:00











                • For me, this worked better as it prints also the entry numbers

                  – Zac
                  Jan 29 '18 at 16:08
















                3














                This should be the right "string"...



                awk -F' '/^menuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                The last pipe delete white space at the beginning of each row and correct the number sequence in accordance with grub number entry.
                Sample output:



                0   Ubuntu
                1 Memory test (memtest86+)
                2 Memory test (memtest86+, serial console 115200)
                3 Windows 10 (su /dev/sdc1)


                If you like to see all entries, also the advanced, you could use



                awk -F' '/(^|| )nuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                but the numbers sequence isn't suitable for use with grub-set-default.






                share|improve this answer





















                • 6





                  This only shows the top level menu. The Advanced Options sub-menu is missing.

                  – WinEunuuchs2Unix
                  Jan 24 '18 at 23:52






                • 1





                  Could you explain what the piped commands do and how it is better than the accepted answer?

                  – Melebius
                  Jan 25 '18 at 10:00











                • For me, this worked better as it prints also the entry numbers

                  – Zac
                  Jan 29 '18 at 16:08














                3












                3








                3







                This should be the right "string"...



                awk -F' '/^menuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                The last pipe delete white space at the beginning of each row and correct the number sequence in accordance with grub number entry.
                Sample output:



                0   Ubuntu
                1 Memory test (memtest86+)
                2 Memory test (memtest86+, serial console 115200)
                3 Windows 10 (su /dev/sdc1)


                If you like to see all entries, also the advanced, you could use



                awk -F' '/(^|| )nuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                but the numbers sequence isn't suitable for use with grub-set-default.






                share|improve this answer















                This should be the right "string"...



                awk -F' '/^menuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                The last pipe delete white space at the beginning of each row and correct the number sequence in accordance with grub number entry.
                Sample output:



                0   Ubuntu
                1 Memory test (memtest86+)
                2 Memory test (memtest86+, serial console 115200)
                3 Windows 10 (su /dev/sdc1)


                If you like to see all entries, also the advanced, you could use



                awk -F' '/(^|| )nuentry / {print $2}' /boot/grub/grub.cfg|cat -n|awk '{print $1-1,$1="",$0}'


                but the numbers sequence isn't suitable for use with grub-set-default.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 11 '18 at 21:45









                Community

                1




                1










                answered Jan 24 '18 at 23:37









                Stefano BizzarriStefano Bizzarri

                313




                313








                • 6





                  This only shows the top level menu. The Advanced Options sub-menu is missing.

                  – WinEunuuchs2Unix
                  Jan 24 '18 at 23:52






                • 1





                  Could you explain what the piped commands do and how it is better than the accepted answer?

                  – Melebius
                  Jan 25 '18 at 10:00











                • For me, this worked better as it prints also the entry numbers

                  – Zac
                  Jan 29 '18 at 16:08














                • 6





                  This only shows the top level menu. The Advanced Options sub-menu is missing.

                  – WinEunuuchs2Unix
                  Jan 24 '18 at 23:52






                • 1





                  Could you explain what the piped commands do and how it is better than the accepted answer?

                  – Melebius
                  Jan 25 '18 at 10:00











                • For me, this worked better as it prints also the entry numbers

                  – Zac
                  Jan 29 '18 at 16:08








                6




                6





                This only shows the top level menu. The Advanced Options sub-menu is missing.

                – WinEunuuchs2Unix
                Jan 24 '18 at 23:52





                This only shows the top level menu. The Advanced Options sub-menu is missing.

                – WinEunuuchs2Unix
                Jan 24 '18 at 23:52




                1




                1





                Could you explain what the piped commands do and how it is better than the accepted answer?

                – Melebius
                Jan 25 '18 at 10:00





                Could you explain what the piped commands do and how it is better than the accepted answer?

                – Melebius
                Jan 25 '18 at 10:00













                For me, this worked better as it prints also the entry numbers

                – Zac
                Jan 29 '18 at 16:08





                For me, this worked better as it prints also the entry numbers

                – Zac
                Jan 29 '18 at 16:08











                2














                I just found out that you can just use auto-complete (pressing tab twice) on commands like grub-reboot or grub-set-default and it'll give you a list of the available boot menu entries. You can then just copy the title you want literally, you don't need to use a number.



                Unfortunately, for some reason my Windows 7 installation doesn't show up that way.






                share|improve this answer






























                  2














                  I just found out that you can just use auto-complete (pressing tab twice) on commands like grub-reboot or grub-set-default and it'll give you a list of the available boot menu entries. You can then just copy the title you want literally, you don't need to use a number.



                  Unfortunately, for some reason my Windows 7 installation doesn't show up that way.






                  share|improve this answer




























                    2












                    2








                    2







                    I just found out that you can just use auto-complete (pressing tab twice) on commands like grub-reboot or grub-set-default and it'll give you a list of the available boot menu entries. You can then just copy the title you want literally, you don't need to use a number.



                    Unfortunately, for some reason my Windows 7 installation doesn't show up that way.






                    share|improve this answer















                    I just found out that you can just use auto-complete (pressing tab twice) on commands like grub-reboot or grub-set-default and it'll give you a list of the available boot menu entries. You can then just copy the title you want literally, you don't need to use a number.



                    Unfortunately, for some reason my Windows 7 installation doesn't show up that way.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 30 '18 at 1:28

























                    answered Mar 29 '18 at 12:42









                    DanManDanMan

                    17711




                    17711























                        0














                        In OpenSuSE grub2-once provides the menu id which you can then use in other commands. Not much help for Ubuntu users though.



                        # grub2-once --list
                        0 openSUSE Leap 42.3
                        1 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default
                        2 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default (recovery mode)
                        3 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default
                        4 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default (recovery mode)
                        5 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default
                        6 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default (recovery mode)
                        7 openSUSE 42.1 (x86_64) (on /dev/sda8)
                        8 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1 (on /dev/sda8)
                        9 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-56-default (on /dev/sda8)
                        10 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-53-default (on /dev/sda8)
                        11 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>Install 42.3 (on /dev/sda8)





                        share|improve this answer


























                        • You should probably share the location that you got grub2-once from

                          – Charles Green
                          Feb 11 at 16:38











                        • Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

                          – Paranoid
                          Feb 11 at 17:35











                        • Seems like a handy utility...

                          – Charles Green
                          Feb 11 at 17:38
















                        0














                        In OpenSuSE grub2-once provides the menu id which you can then use in other commands. Not much help for Ubuntu users though.



                        # grub2-once --list
                        0 openSUSE Leap 42.3
                        1 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default
                        2 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default (recovery mode)
                        3 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default
                        4 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default (recovery mode)
                        5 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default
                        6 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default (recovery mode)
                        7 openSUSE 42.1 (x86_64) (on /dev/sda8)
                        8 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1 (on /dev/sda8)
                        9 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-56-default (on /dev/sda8)
                        10 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-53-default (on /dev/sda8)
                        11 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>Install 42.3 (on /dev/sda8)





                        share|improve this answer


























                        • You should probably share the location that you got grub2-once from

                          – Charles Green
                          Feb 11 at 16:38











                        • Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

                          – Paranoid
                          Feb 11 at 17:35











                        • Seems like a handy utility...

                          – Charles Green
                          Feb 11 at 17:38














                        0












                        0








                        0







                        In OpenSuSE grub2-once provides the menu id which you can then use in other commands. Not much help for Ubuntu users though.



                        # grub2-once --list
                        0 openSUSE Leap 42.3
                        1 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default
                        2 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default (recovery mode)
                        3 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default
                        4 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default (recovery mode)
                        5 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default
                        6 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default (recovery mode)
                        7 openSUSE 42.1 (x86_64) (on /dev/sda8)
                        8 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1 (on /dev/sda8)
                        9 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-56-default (on /dev/sda8)
                        10 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-53-default (on /dev/sda8)
                        11 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>Install 42.3 (on /dev/sda8)





                        share|improve this answer















                        In OpenSuSE grub2-once provides the menu id which you can then use in other commands. Not much help for Ubuntu users though.



                        # grub2-once --list
                        0 openSUSE Leap 42.3
                        1 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default
                        2 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.172-86-default (recovery mode)
                        3 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default
                        4 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.165-81-default (recovery mode)
                        5 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default
                        6 Advanced options for openSUSE Leap 42.3>openSUSE Leap 42.3, with Linux 4.4.159-73-default (recovery mode)
                        7 openSUSE 42.1 (x86_64) (on /dev/sda8)
                        8 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1 (on /dev/sda8)
                        9 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-56-default (on /dev/sda8)
                        10 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>openSUSE Leap 42.1, with Linux 4.1.39-53-default (on /dev/sda8)
                        11 Advanced options for openSUSE 42.1 (x86_64) (on /dev/sda8)>Install 42.3 (on /dev/sda8)






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 11 at 17:45

























                        answered Feb 11 at 13:40









                        ParanoidParanoid

                        11




                        11













                        • You should probably share the location that you got grub2-once from

                          – Charles Green
                          Feb 11 at 16:38











                        • Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

                          – Paranoid
                          Feb 11 at 17:35











                        • Seems like a handy utility...

                          – Charles Green
                          Feb 11 at 17:38



















                        • You should probably share the location that you got grub2-once from

                          – Charles Green
                          Feb 11 at 16:38











                        • Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

                          – Paranoid
                          Feb 11 at 17:35











                        • Seems like a handy utility...

                          – Charles Green
                          Feb 11 at 17:38

















                        You should probably share the location that you got grub2-once from

                        – Charles Green
                        Feb 11 at 16:38





                        You should probably share the location that you got grub2-once from

                        – Charles Green
                        Feb 11 at 16:38













                        Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

                        – Paranoid
                        Feb 11 at 17:35





                        Although this is about ubuntu I use OpenSuSE and it installs all grub v2 commands as grub2-????. When I was searching for how to get the grub menu list this is the question that is found irrespective of the linux flavour so I figured my response may be helpful to others. What I didn't realise is that the grub2-once command appears to be unique to suse (no equivalent grub-once in ubuntu). I'll amend my answer to reflect this

                        – Paranoid
                        Feb 11 at 17:35













                        Seems like a handy utility...

                        – Charles Green
                        Feb 11 at 17:38





                        Seems like a handy utility...

                        – Charles Green
                        Feb 11 at 17:38











                        0














                        An other awk one-liner which handles submenus :



                         awk -F' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /tmenuentry / {print "t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg


                        This might be fallible thought, because t is used to identify submenu entries.






                        share|improve this answer




























                          0














                          An other awk one-liner which handles submenus :



                           awk -F' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /tmenuentry / {print "t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg


                          This might be fallible thought, because t is used to identify submenu entries.






                          share|improve this answer


























                            0












                            0








                            0







                            An other awk one-liner which handles submenus :



                             awk -F' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /tmenuentry / {print "t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg


                            This might be fallible thought, because t is used to identify submenu entries.






                            share|improve this answer













                            An other awk one-liner which handles submenus :



                             awk -F' '$1=="menuentry " || $1=="submenu " {print i++ " : " $2}; /tmenuentry / {print "t" i-1">"j++ " : " $2};' /boot/grub/grub.cfg


                            This might be fallible thought, because t is used to identify submenu entries.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 27 at 13:58









                            MaxMax

                            32115




                            32115






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Ask Ubuntu!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f599208%2fhow-to-list-grubs-menuentries-in-command-line%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

                                is 'sed' thread safe

                                How to make a Squid Proxy server?