Everyone wants their scripting to be more interactive, especially users. Users want to feel like they’re working with a modern piece of software instead of something cobbled together from old batch files and cryptic one-liners. They also want a self-documenting script that requires as little documentation as possible. If these things sound like what you’ve been waiting for, then read on! A shell script is a perfect place for user input because it is such a simple programming construct. It essentially consists of a series of commands that have been given their own names

Advertisement

The Linux read command provides you the option to prompt for user input. Once the user-provided input hits enter, the command store provided input to a variable. That 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.

Taking User Input in Bash

When writing a shell script, it may be helpful to prompt the user for input. This will allow you to get a specific value that you can use in your script as you see fit. It also allows you to make your script interactive by prompting the user to enter values such as directories and filenames.

In bash, you can prompt the user with a read command.

read my_var 

Here “read” is the Linux command and “my_var” is the variable, that will store the input value.

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 $my_var

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

You can also add a message at the input prompt for user convenience. It looks like this:

read -p "What is your name? " my_var 

In this example, the read command is followed by a -p. This tells the script to prompt the user for input. The text inside the quotes after the -p is displayed to the user when they are prompted. By using read in your shell scripts, you can prompt the user for input and store the results in a variable. You can also use read to read in one value using a variable to prompt the user for another value. You can use read for many things, but it’s most useful for reading user input.

Shell Script to Prompt for User Input

Write a sample shell script to prompt 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:

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.

Again execute the same script and see the outputs:

./input.sh

The output is self-explanatory. The inputed value is stored in variable and then prints it.

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.

Save the file and close it.

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

Conclusion

A shell script is a perfect place for user input because it is such a simple programming construct. It essentially consists of a series of commands that have been given their own names. 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.

Share.

8 Comments

  1. 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.

  2. 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.

  3. 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.

  4. 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 ] “

Exit mobile version