User Input In Bash
Use read command for interactive user input in bash scripts. This is helpful for taking input from users at runtime.
$ read variable_name
There is no need to specify any datatype for the variables with the read operation. This simply takes an input of any data type values. Try with a sample command. Open bash shell terminal and type 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.
#!/bin/bash echo "Enter your name:" read myname echo "Hello" $myname
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 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.
read -p "Enter your username: " myname
read -sp "Enter your password: " mypassword
echo -e "\nYour username is $myname and Password is $mypassword"