What Command Can You Use to Retrieve User Input Into a Variable in a Bash Script

Read User Input

In this topic, we will learn how to read the user input from the terminal and the script.

To read the Fustigate user input, nosotros use the congenital-in Bash control chosen read. It takes input from the user and assigns information technology to the variable. It reads merely a single line from the Bash trounce. Below is the syntax for its implementation.

Syntax

Follow the given examples to read user input from the Bash Script:

Example 1:

In this example, we read both the single and multiple variables from the Bash Script by using read command.

Program:

Meet the Bash Panel:

Read User Input

Output:

Read User Input

What will happen if we don't pass whatsoever variable with the read command?

If we don't pass whatsoever variable with the read command, then nosotros tin laissez passer a built-in variable called REPLY (should exist prefixed with the $ sign) while displaying the input. It can be explained using the below program:

Plan:

On Bash Console:

Read User Input

Output:

Read User Input

Instance two:

In this example, we enter the input on the aforementioned PROMPT by using the -p control line option as follows:

Program:

See the Fustigate Console:

Read User Input

Output:

Read User Input

Example 3:

This instance is to keep the input on silent mode, such that whatever be a user input on the command line volition exist subconscious to others.

So, we pass a username and hide the countersign (silent fashion) by using the command line options (-due south, -p) unremarkably as follows:

Where -south allows a user to keep the input on silent mode and -p to input on newly control prompt.

Program:

See the Bash Console:

Read User Input

Output:

Read User Input

NOTE: At the vthursday line of the script, we have given a blanked line with echo command, because if nosotros exercise not brand it blank and so, it will requite output with both the password and username on the same PROMPT as the beneath image.

Read User Input

Then, write your script by adding a blank echo command line.

Example iv: This instance is to enter multiple inputs using an assortment. So employ the -a command line option as follows:

Where -a helps script to read an assortment, and variable_name refers to an array.

Program:

See the code on Bash Console:

Read User Input

Output:

Read User Input

guentherbelve1987.blogspot.com

Source: https://www.javatpoint.com/bash-read-user-input

0 Response to "What Command Can You Use to Retrieve User Input Into a Variable in a Bash Script"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel