In this tutorial, you will learn how to read the users input on bash script and then do something with the input.
To read users input in bash we need to use the
read command as shown in the example below
In this script we are using echo command to tell the users what they need to do, in this case the user needs to enter his/her first and lastname, and then based on the input that the user inserted, the script will display the welcome message " Welcome to OwlHowTo $firstname $lastname"
#!/bin/bash echo "Please enter your firstname" read firstname echo "Please enter your lastname" read lastname echo "Welcome to OwlHowTo" $firstname $lastname
Let's try to run this script and see how it works
Open your terminal, and run, assuming you have already made the script executable
owlhowto-welcome.sh is the name of the bash script, you can choose any name you want for this script, just make sure to make the script executable first before running it.
This is how the script looks like
It works fine, but it is a little hard to read because there's no space between new lines, to print lines to a new line we can use the
echo -e command
#!/bin/bash echo -e "\nPlease enter your firstname\n" read firstname echo -e "\nPlease enter your lastname\n" read lastname echo -e "\nWelcome to OwlHowTo" $firstname $lastname
Now lines will have space between them, and it won't be that confusing. Notice this time I submitted only my firstname and left last name empty, and the script worked again because we are not checking whether the user is inserting only firstname or lastname. Since we are not checking if the input is valid or empty, then it is possible to enter basically just "space" as firstname and lastname.
And the script will display
Welcome to OwlHowTo space space
To check if the input that is being submitting is empty we can use the -z operator. For example, to check if the firstname string is empty or not
if [[ -z "$firstname" ]];
we can use the same operator to check the lastname too
if [[ -z "$lastname" ]];
so if firstname or lastname will be empty then we will display a message "Invalid input, you must enter a name and lastname"
Instead of checking only of the variables if it contains empty string, we can check both at the same time
if [[ -z "$firstname" ]] || [[ -z "$lastname" ]]; then echo "Invalid input, you must enter a name and lastname"
So now our script checks for name and lastname input, and if one of them is empty then the script will stop working. If both variables contain a string then the script will show the welcome message.
The whole script
#!/bin/bash echo -e "\nPlease enter your firstname\n" read firstname echo -e "\nPlease enter your lastname\n" read lastname if [[ -z "$firstname" ]] || [[ -z "$lastname" ]]; then echo "Invalid input, you must enter a name and lastname" else echo -e "\nWelcome to OwlHowTo $firstname $lastname\n" fi
You can also customize this script to your preference, for example you can add colors and another function that runs once the user has submitted first and last name.
In this tutorial, we explained step by step how to write a bash script that reads the users input and display that input to the user. We also explained how to check for empty strings when users submit their first and lastname, and show a message based on the input when empty or not.