User Input In Bash
Linux read command is used for interactive user input in bash scripts. This helps take input from users at runtime.
Syntax:
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.
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.
Example 2:
Let’s know some more options used with the read command. For example to prompt some message with the read command.
Use -s
to input without displaying it on the screen. This is helpful to take password input in the script.
Now, put both commands in a shell script and execute.
1 2 3 4 5 6 | #!/bin/bash read -p "Enter your username: " myname read -sp "Enter your password: " mypassword echo -e "\nYour username is $myname and Password is $mypassword" |
0 Comments
CAN FEEDBACK
Emoji