For system administrators, shell scripts provide an easy and efficient way of executing multiple commands. A common convention in shell scripts is to read input from the user. This input can either be a single character or an entire line.
In this tutorial, we’ll focus on reading a single character. This approach allows us to capture and handle the user input but with less typing required from the user.
2. Reading User Input in a Shell Script
In Linux, we use the read command to capture lines of user input. Additionally, we can modify it to read only a single character.
2.1. Understanding the read Command
In shell scripting, the read command captures user input and stores it in a variable. By default, it reads the entire line of user input up until the user presses the Enter key. As a consequence, a unique approach is needed to only capture a single character using read.
To understand, let’s begin by creating a test.sh file and pasting the content below:
#!/bin/bash # User prompt echo -n "Please type in your gender [M/F]: " # Read the user input read gender #Print the user input echo "Your answer: $gender"
In this example, we instruct the user to provide their gender, either male (M) or female (F):
- echo -n – echo command prints out the text argument Please type in your gender (M/F): with no newline after
- read gender – reads the user’s input and stores it in the variable $gender
- echo “Your answer: $gender” – displays the value of the variable $gender
So, let’s test out the shell script:
$ bash test.sh Please type in your gender [M/F]: Male Your answer: Male
Above, the script is able to prompt the user, read the entire input Male into the variable gender, and also display the variable contents.
In the next section, we explore reading only a single character.
2.2. Reading a Single Character
Here, we need to modify the behavior of the read command. In detail, we’ll utilize the -n option to declare the limit of characters to be read.
First, let’s update some lines in our test.sh file:
#!/bin/bash ... # Read the user input read -n 1 gender #Print the user input echo -e "\nYour answer: $gender"
In this script, the -n option used with read allows us to limit the input to a specific number of characters. In this case, the read command can only capture one character. On the other hand, the -e option enables the echo command to interpret escape characters, in this case, a newline (\n):
$ bash test.sh Please type in your gender [M/F]: M Your answer: M
Now, when we enter the first character, the script immediately reads this character. Then, it moves to a new line printing out Your answer: M. To clarify, it doesn’t wait for us to press the Enter key.
2.3. Handling the Single Character User Input
Here, we’ll discuss how to handle the single character input that’s now stored in the variable $gender.
Normally, users have the option to input anything including an unexpected character. That’s why it’s always important to declare conditional statements to handle the different inputs.
To illustrate, let’s add conditional statements to our test.sh file:
#!/bin/bash ... #Print the user input case "$gender" in [mM]) echo -e "\nYour answer: Male" ;; [fF]) echo -e "\nYour answer: Female" ;; *) echo -e "\nInvalid choice: $gender" ;; esac
In this example, we replace the command under #Print the user input with the instructions above.
Depending on the input, the case statement dictates what command the script should execute. For instance, if the variable $gender contains either m or M, the script prints Your answer: Male. On the other hand, if this user input is f or F, then we get Your answer: Female. Equally important, the script returns Invalid choice: $gender for any other user input.
So, let’s check out the result for an invalid input:
$ bash test.sh Please type in your gender [M/F]: n Invalid choice: n
In this situation, we’re assuming that the user incorrectly inputs n. Now, with the help of the case statement, the script automatically notices this and displays Invalid choice: n.
In this article, we explored reading a single character in a shell script using the read command.
First, we discussed the default behavior of this command. Then, we utilized its -n option and modified it to read only a single character.
Finally, we took a look at conditional statements to handle the user input.