User Input In Bash

User Input In Bash

 User Input In Bash

Linux read command is used for interactive user input in bash scripts. This helps take input from users at runtime.

read [options] variable_name

Example 1:

There is no need to specify any data type for the variables with the read operation. This simply takes an input of any data type values. Try with a sample command. Open the bash shell terminal and type the following command.

read myvar

The above command will wait for user input. so just type anything and press enter. Let’s create a simple shell script to read your name and print.


echo "Enter your name:"
read myname
echo "Hello" $myname

Example 2:

Let’s know some more options used with the read command. For example to prompt some message with the read command.

read -p "Enter your username: " myname

Use -s to input without displaying it on the screen. This is helpful to take password input in the script.

read -sp "Enter your password: " mypassword

Now, put both commands in a shell script and execute.


Post a Comment