How to Prompt for User Input in Shell Script

Introduction:

Everyone wants to know about How to Prompt for User Input in Shell Script, especially users, wants their programming to be more participatory. Instead of using software that was assembled from obscure one-liners and outdated batch files, users want to feel as though they are dealing with a contemporary piece of software. They also want a script that can document itself as much as possible on its own. Continue reading these items How to Prompt for User Input in Shell Script! Because a shell script is such a basic programming concept, it’s the ideal spot for user input. It is basically just a list of instructions with names attached to them.

How to Prompt for User Input in Shell Script:

The read command in Linux shells enables you to read one or more user inputs into your shell script. Its syntax is as follows.

read <variable_name>

You must specify the variable in which you wish to save user input after the read operation.

We’ll examine a few typical read command use cases.

1. Read Single Input:

In this cases, we simply prompt user for single input and store it in a single variable.

#/home/test.sh
#!/bin/sh

echo "enter name:"
read name
echo "you entered $name"

The following prompt will appear when you run the script mentioned above. Upon input, the script will echo the user’s input back to the screen and store it in the $name variable.

$ /home/test.sh
enter name:fedingo
you entered fedingo

2. Read Multiple Inputs

Read command can also be used to ask for several inputs. In this instance, the read command requires you to specify each variable name one after the other.

read variable1 variable2 variable3 ...

In this instance, the user must input the values for each variable one after the other in a format separated by spaces, and then hit the enter key to submit the data after typing the value for the final variable.

#/home/test.sh
#!/bin/sh

echo "enter first name, last name and age:"
read first_name last_name age
echo "you entered $first_name, $last_name, $age

The script mentioned above will ask you to provide your age, last name, and first name when it runs. It will store the three values you enter in a space-separated format in the shell variables $first_name, $last_name, and $age, and then echo those values back to the screen.

$ /home/test.sh
enter first name, last name and age:john doe 42
you entered john, doe, 42

You may also use an array to hold user input values that are received using the read command if you are unsure of how many inputs to expect from the user or if you wish to accept a variable amount of inputs. In this instance, in order to store input values in an array, you must use the -a option after the read command. This is an example of how to store the values for the first, last, and age names in a details array.

#/home/test.sh
#!/bin/sh

echo "enter first_name, last_name and age"
read -a details
echo "you entered ${details[0]},${details[1]},$details{details[2]}

You will be asked for your age, last name, and first name when you run the script mentioned above. These values will be entered and saved in the details array.

$ /home/test.sh
enter first name, last name, age:john doe 42
you entered john, doe, 42

Since you are using an array, user can enter any number of inputs and your shell script will capture them properly. It is a great way if you want to accept a list of values such as names, places, etc.

3. Accept Input Without Variable

If you don’t mention variable name, then shell script will store it in $REPLY system variable. Here is an example

#/home/test.sh
#!/bin/sh

echo "enter name"
read
echo "you entered $REPLY"

When you run the above command, you will be prompted for input. The string you enter will be stored in $REPLY variable, and echoed back to your screen.

$ /home/test.sh
enter name: fedingo
you entered fedingo

4. Hide User Input

On occasion, you might want to conceal the user’s typing input. For instance, you’ll need to conceal it if you wish to ask the user for private information like passwords. To accomplish this, use the read command’s -sp option, where -s denotes quiet mode and -p, prompt. The user’s input will not be displayed by the shell when it is in silent mode. Here’s an illustration.

#/home/test.sh
#!/bin/sh

echo "enter password:"
read -sp password
echo "you entered $password"

When you run the above command, you will be prompted for password. What you type will not be visible until you hit enter key. At that time, the script will echo back the full input you entered.

$ /home/test.sh
enter password:
you entered 1234

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.


Get more information about
How to Configuring Awall on Alpine Linux latest Guide

Leave a Reply

Your email address will not be published. Required fields are marked *