Limiting The Number of Entered Characters In read prompt

Last Updated on: September 3, 2020

We previously looked at the way we could utilize the read prompts in bash, here is the script we wrote in out last guide:

					

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
exit 0

What if we do something as follows:

					

#!/bin/bash
read -p "What is Your Name? " name
echo "Your Name is $name"
read -n1 -p "Press any key to exit "
echo
exit 0

Now, when the script displays the name, it will pause until you press any key, this is possible by using the -n option followed by an integer, in this case, we specify it to accept just 1 character.

Comment policy: Respectful and beneficial comments are welcome with full open hands. However, all comments are manually moderated and those that doesn't relate with what the passage is saying or offensive comments would be deleted. Thanks for understanding!

Leave a Reply

Your email address will not be published. Required fields are marked *