The Linux read command provides you the option to prompt for user input. Once the user-provided input and hits enter, the command store provided input to a variable. Which can be used later in the shell scripts.
In this tutorial, you will learn, how to prompt for user input in a shell script. Also, help you with accepting passwords from users in hidden characters.
Take Input on Termianl
Let’s begin with input directly on terminal. Open a terminal on your system and type:
read x
Here read is the Linux command and “x” is the variable, where the input value will be stored.
Hit enter after typing the above command. You will see a blank line without a prompt. It means the shell is waiting for your input. Type some text and hit enter button. You will find the prompt again.
Now, verify that the input value is stored in a defined variable. To confirm it, print the variable value with the echo command.
echo $x
There is no need to define any data type for variables. Shell automatically adjusts the type based on the user input.
Lets’ go with a sample shell script to take user input.
Shell Script to Prompt User Input
Write a sample shell script to prompt for the user input and store it in a variable. Make sure to print a proper message on the screen before prompting for input. Store the input value in a variable and then print it in with a welcome message.
Create a new file “input.sh” and write below script:
1 2 3 4 5 | #!/bin/bash echo "Enter Your Name: " read x echo "Welcome ${x}!" |
Save your file and close it.
chmod +x input.sh
./input.sh
In the above script, you found that the input is prompted in a new line. To make it better, use the -p
option to print the message before input with the read command. With this, we will skip the first echo line.
Edit your script and make the following changes.
1 2 3 4 | #!/bin/bash read -p "Enter Your Name: " x echo "Welcome ${x}!" |
Again execute the same script and see the outputs:
./input.sh
How to Input a Password in Shell Script
The password input should be hidden on the screen, similar to what the Linux shell does when setting a password on the screen. Passwords must not be visible on the screen.
The read command provides the silent mode option with “-s” to hide the input characters from the screen. Let’s create a sample script to take password input from the user.
vim input-pass.sh
Add the below script.
1 2 3 4 | #!/bin/bash read -s -p "Enter a Password: " x echo "Your password is - $x" |
Save the file and close it.
chmod +x input-pass.sh
./input-pass.sh
Conclusion
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.
8 Comments
Hi Rahul,
Currently im looking for pass the input value to grep command to fetch the output. at end im facing below message also not sure what i passed is correct.
read -r “client name” input
case $input in
jobs -report | grep “$input”
jobs command doesnt have any parameter to pass so i need to grep the from -report. so how to pass the input read value to grep to display the output.
Excellent article. Keep posting such kind of information on your page.
Im really impressed by your blog.
Hello there, You have done an incredible job.
I will certainly digg it and individually recommend to my friends.
I am sure they will be benefited from this website.
Hi,
Can you let me know is there an alternative of “read” command. My code is like this:
————————————–
while read line
do
print “Do you want to continue”
read flag
…..
…..
…..
done > file.txt
———————————
Here the second read command (to get the user input) is not working.
Please provide a solution if you have.
Thanks.
great post exactly what i was looking for
Hello Rahul Sir ,
i want help for writing code
Q. take input from user in form of yes or no .
-> “Do you want to know in which Directory you are [ y | n ] “
This will help you: https://tecadmin.net/bash-script-prompt-to-confirm-yes-no/
Hello Rahul i need help with shell program i’m novice.
You have very nice collection of linux commands Rahul.