Bash Script wait for user Input Command

If you are writing a bash script and you are fetching input from the user and you want to wait for the user input, then you can make use of the read command with a -p (prompt) option.

Let's take a look at an example script.

Example:

#!/bin/bash

read -p "Enter Your Name: " user_name 
echo "Hello: $user_name"
Output:
./script.sh 
Enter Your Name: Sam
Hello: Sam

You will see that the script waits for user to enter their name and only returns when enter key is pressed.

Bash Script wait for user Input Command Example

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!