Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Bash Script – Prompt to Confirm (Y/N, YES/NO)

    Bash Script – Prompt to Confirm (Y/N, YES/NO)

    RahulBy RahulAugust 16, 20152 Mins ReadUpdated:January 6, 2022

    Question – How to add [Y/n] confirmation in our own shell scripts? How to take input from the user as Yes/No/Y/N in bash?

    Sometimes you have seen that shell scripts prompt [Y/n] or [Yes/No] to user for confirmation. This is useful to know if a user wants to proceed with the remaining steps or not. You can also add the same function to your script. This article will help you with examples of (Bash Script – Prompt to Confirm (Y/N, YES/NO)) this type of inputs.

    #1. Bash (Yes/No) Prompt

    This example code will prompt for confirming once if you give wrong input, the program will exit with status 1. The below example code will accept only Y or n or yes or no (Not case-sensitive).

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #!/bin/bash
     
    read -r -p "Are You Sure? [Y/n] " input
     
    case $input in
          [yY][eE][sS]|[yY])
                echo "You say Yes"
                ;;
          [nN][oO]|[nN])
                echo "You say No"
                ;;
          *)
                echo "Invalid input..."
                exit 1
                ;;
    esac

    #2 Prompt for Confirmation (in Loop)

    Another example is a shell script with a while loop, that will prompt for confirmation until you give proper input like (Y, N, YES, or NO). If you give the wrong input, it will again prompt for correct input and repeat the same steps. This example will accept only Y or N or YES or NO (Not case-sensitive).

    Shell
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #!/bin/bash
     
    while true
    do
          read -r -p "Are You Sure? [Y/n] " input
     
          case $input in
                [yY][eE][sS]|[yY])
                      echo "Yes"
                      break
                      ;;
                [nN][oO]|[nN])
                      echo "No"
                      break
                      ;;
                *)
                      echo "Invalid input..."
                      ;;
          esac      
    done

    Conclusion

    This tutorial helped you to create a shell script, that prompts for Yes/No for user confirmation. This will help you go create a bash shell script to confirm user to continue.

    bash script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Create Binary File from Shell Script
    Next Article How to Install Simple Screen Recorder on Ubuntu & LinuxMint

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    4 Comments

    1. ajay on June 7, 2019 8:02 am

      In the 2nd example. loops goes on infinity even if user type yes or no.
      use break statement to terminate the loop upon valid input.

      while true
      do
      read -r -p “Are You Sure? [Y/n] ” input

      case $input in
      [yY][eE][sS]|[yY])
      echo “Yes”
      break
      ;;
      [nN][oO]|[nN])
      echo “No”
      break
      ;;
      *)
      echo “Invalid input…”
      ;;
      esac
      done

      Reply
    2. Raj on December 16, 2018 3:07 am

      It goes to infite loop. didn’t ask for user input !

      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process
      test.ksh[4]: read: no query process

      Reply
    3. Jeff Fritz on March 28, 2017 6:29 pm

      Thanks for your page, but there is a bug here –

      This – is incorrect. You need to drop the semicolon at the end of the line ‘while true;’
      while true:
      do

      This is correct –
      while true
      do

      Reply
      • Rahul K. on March 29, 2017 2:23 am

        Thanks Jeff,

        The article has been updated.

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.