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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap