facebook youtube pinterest twitter reddit whatsapp instagram

Limiting The Number of Entered Characters In read prompt

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.