Prompt a user for input from a shell script (Bash shell script), and then read the input the user provides.

How to prompt and read user input
=================================

**Question**: How do I prompt a user for input from a shell script (Bash shell script), and then read the input the user provides?

**Answer**: I usually use the shell script `read` function to read input from a shell script. Here are two slightly different versions of the same shell script. 

### Version 1

This first version prompts the user for input only once, and then dies if the user doesn't give a correst `Y/N` answer:

	#!/bin/sh
	
	# (1) prompt user, and read command line argument
	read -p "Run the cron script now? " answer
	
	# (2) handle the command line argument we were given
	while true
	do
		case $answer in
		[yY]* ) /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php
				echo "Okay, just ran the cron script."
				break;;
		[nN]* ) exit;;
			* ) echo "Dude, just enter Y or N, please."; break ;;
		esac
	done

### Version 2

This second version stays in a loop until the user supplies a `Y/N` answer:

	#!/bin/sh
	
	while true
	do
		# (1) prompt user, and read command line argument
		read -p "Run the cron script now? " answer
		
		# (2) handle the input we were given
		case $answer in
			[yY]* ) /usr/bin/wget -O - -q -t 1 http://www.example.com/cron.php
					echo "Okay, just ran the cron script."
					break;;
			[nN]* ) exit;;
				* ) echo "Dude, just enter Y or N, please.";;
		esac	
	done

---

[Bash shell script - how to prompt and read user input](http://alvinalexander.com/linux-unix/shell-script-how-prompt-read-user-input-bash)