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»How to Prompt for User Input in Linux Shell Script

    How to Prompt for User Input in Linux Shell Script

    RahulBy RahulNovember 5, 20143 Mins ReadUpdated:June 7, 2022

    The Linux read command provides you the option to prompt for user input. Once the user-provided input and hits enter, the command store provided input to a variable. Which can be used later in the shell scripts.

    In this tutorial, you will learn, how to prompt for user input in a shell script. Also, help you with accepting passwords from users in hidden characters.

    Take Input on Termianl

    Let’s begin with input directly on terminal. Open a terminal on your system and type:

    read x 
    

    Here read is the Linux command and “x” is the variable, where the input value will be stored.

    Hit enter after typing the above command. You will see a blank line without a prompt. It means the shell is waiting for your input. Type some text and hit enter button. You will find the prompt again.

    Now, verify that the input value is stored in a defined variable. To confirm it, print the variable value with the echo command.

    echo $x
    

    There is no need to define any data type for variables. Shell automatically adjusts the type based on the user input.

    Lets’ go with a sample shell script to take user input.

    Shell Script to Prompt User Input

    Write a sample shell script to prompt for the user input and store it in a variable. Make sure to print a proper message on the screen before prompting for input. Store the input value in a variable and then print it in with a welcome message.

    Create a new file “input.sh” and write below script:

    1
    2
    3
    4
    5
    #!/bin/bash
     
    echo "Enter Your Name: "
    read x
    echo "Welcome ${x}!"

    Save your file and close it.

    chmod +x input.sh
    ./input.sh
    

    Prompt for User Input in Bash

    In the above script, you found that the input is prompted in a new line. To make it better, use the -p option to print the message before input with the read command. With this, we will skip the first echo line.

    Edit your script and make the following changes.

    1
    2
    3
    4
    #!/bin/bash
     
    read -p "Enter Your Name: " x
    echo "Welcome ${x}!"

    Again execute the same script and see the outputs:

    ./input.sh
    

    Read User Input in Shell Script

    How to Input a Password in Shell Script

    The password input should be hidden on the screen, similar to what the Linux shell does when setting a password on the screen. Passwords must not be visible on the screen.

    The read command provides the silent mode option with “-s” to hide the input characters from the screen. Let’s create a sample script to take password input from the user.

    vim input-pass.sh 
    

    Add the below script.

    1
    2
    3
    4
    #!/bin/bash
     
    read -s -p "Enter a Password: " x
    echo "Your password is - $x"

    Save the file and close it.

    chmod +x input-pass.sh
    ./input-pass.sh
    

    Read Password in Shell Script

    Conclusion

    This how-to guide helped you to understand the user input in shell scripts. Additionally provides you an example to take input of a password in the shell script.

    bash input read shell script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Recover Outlook Data Using Systools Outlook Recovery
    Next Article How to Use Newline character ( \n ) in Bash Shell

    Related Posts

    Backup MySQL Databases to Amazon S3 (Shell Script)

    Updated:May 28, 20222 Mins Read

    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

    Creating Menu in Shell Script (Linux Select Command)

    Updated:May 31, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    8 Comments

    1. Ray on December 22, 2020 8:00 am

      Hi Rahul,

      Currently im looking for pass the input value to grep command to fetch the output. at end im facing below message also not sure what i passed is correct.

      read -r “client name” input
      case $input in
      jobs -report | grep “$input”

      jobs command doesnt have any parameter to pass so i need to grep the from -report. so how to pass the input read value to grep to display the output.

      Reply
    2. Payday online on December 30, 2019 3:58 am

      Excellent article. Keep posting such kind of information on your page.

      Im really impressed by your blog.
      Hello there, You have done an incredible job.

      I will certainly digg it and individually recommend to my friends.

      I am sure they will be benefited from this website.

      Reply
    3. shubham on September 22, 2019 7:31 am

      Hi,

      Can you let me know is there an alternative of “read” command. My code is like this:
      ————————————–
      while read line
      do
      print “Do you want to continue”
      read flag
      …..
      …..
      …..
      done > file.txt
      ———————————

      Here the second read command (to get the user input) is not working.
      Please provide a solution if you have.

      Thanks.

      Reply
    4. ponix on September 19, 2019 7:52 pm

      great post exactly what i was looking for

      Reply
    5. Rahul Nakade on August 12, 2019 5:28 pm

      Hello Rahul Sir ,
      i want help for writing code
      Q. take input from user in form of yes or no .
      -> “Do you want to know in which Directory you are [ y | n ] “

      Reply
      • Rahul on August 13, 2019 9:56 am

        This will help you: https://tecadmin.net/bash-script-prompt-to-confirm-yes-no/

        Reply
    6. Muhammad Buhari Maiwada on April 9, 2019 9:21 am

      Hello Rahul i need help with shell program i’m novice.

      Reply
    7. ratan on April 23, 2017 2:09 pm

      You have very nice collection of linux commands Rahul.

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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