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.

Related Post(s)

  • Suppressing Sensitive Entered Text/String In Bash

    So far in our examples or scripts, we have written in the previous guides, we have no way of controlling what is visible to user, which is fine if we have no sensitive data such as passwords. As soon

  • read prompts in bash script

    We've previously taken a quick look at the builtin read command, where we use the built-in read to populate the $REPLY variable, which holds the value of read when a variable is not supplied to read

  • Using read in a Bash Script

    We previously looked at using echo with options in a shell, in this guide we would combine it with read command to prompt for user input. Create and open up a new text file: nano $HOME/bin/hw3.sh Ad