£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

# User Input

How to input into bash scripts
7.5
In the previous step, we looked at defining variables and storing their values within a Bash script. Now, we’ll look at how we can ask the user to provide those values interactively on the command line. In this video, we’ll walk you through how you can use Bash to request and capture user input interactively as part of your script. We’ll create a new script, which will perform the following actions. First, prompt the user to provide their name using echo, second, store the user input as a variable using read, and then return that variable in a greeting back to the user, again using echo. We’ll call this new script whoami.sh, and it will contain the following commands.
54.1
Let’s take a closer look at what these commands do. First, as always, we’ll include the shebang at the top of our script. This tells us that we want to invoke the Bash Interpreter. Next, we send a request for the user to enter their name in the terminal using echo. In our script, we will then use the read command to capture the user input. For this, we use the command read, followed by the variable name where we want to store the user’s response. It’s always worth noting that, when we declare a variable– in this case, name– for the first time, we don’t need to use the dollar symbol as a prefix.
95.6
Finally, we’ll return the user input back to the user on the command line using echo. The user’s response was stored in a variable called name, so I’ll need to use this as part of the command to return that input. Below, there are some good practises to follow for reading variables. First, always prefix the variable name with the dollar symbol, enclose the variable name in curly braces to avoid unwanted consequences, and use double quotes for the same reason. Let’s have a look at how this script will work in practise. So we’re going to use nano, as before, as our text editor in order to create and add commands to our Bash script.
140.6
Once your text editor is open, you copy and paste the text in that has been provided as part of this step. You then save your script file and exit your text editor. We’re then ready to update the permissions on our file using chmod. Remember the plus x option in order to tell the system that we want to make our script executable. Once we’ve run that command, we’re ready to execute our script, remembering that we need to prefix this with a dot forward slash. When we hit Enter, this will then prompt us for our name. And so as user input, I will enter my name. So Victoria.
180.8
And then when I’ve hit Enter, it will take this, store it as a variable as part of our script, and return it back to us as a string in the terminal. In this example, we used read in its simplest form in order to capture a single user input and store it as a single variable. In this case, name. The syntax for this was read, followed by the name of the variable in which we were storing their input. But what happens if you want to ask the user for multiple inputs? In this case, we need to split that input and store it as multiple variables.
217.1
The syntax here shows how we would store two user inputs in two variables using the read command. To demonstrate this, let’s create a new script called food.sh, where we ask the user for multiple inputs. In this case, we’re going to ask them to enter two of their favourite foods. When the user enters their two foods, which will be on a single line separated by a space, these get stored as two variables, food1 and food2. And then within the script, we ask it to return both of those values back to the user using echo.
258.1
So for example, if the user wants to say that their favourite foods were apples and pears entered on the same line, separated by a space, apples would be stored into food1, our first variable, and pears into food2 as our second variable. The script would then print that their favourite foods are apples and pears. Let’s see how this works in practise. As before, we use nano to create an edit our script called food.sh. We’ll copy and paste the commands that have been provided as part of this step. We’ll save our file and exit the editor.
298.2
We’ll then change the permissions on our file to make it executable, and then we’ll run our script, which will prompt us to enter our two favourite foods. You’ll see here that we’re going to enter two food separated by a space. In this case, apples and pears. We’re then going to return this back to the user. So here, you’ll see that your favourite foods are apples and pears. Remember that we split our input by that space, so apples were stored as our first variable and pears were stored as the second. So when we return it as part of that echoed string, it’s then split by an and.
339.3
In this example, the user gave the same number of responses as we had variables to store them. But what would happen if the user had given three foods instead of just two? Say, for example, apples, pears, and oranges. Now, when we split their response by the space character, what would happen is apples would be stored to the variable food1, but both pears and oranges with the space between them would be stored as a string to food2. This means that, when we provide more options or responses than we have designated variables, any extra text will remain as part of that final variable.
382.9
As we’ve covered, the read command interactively captures user input, and so far, we’ve been using it in its simplest form. There are several options which are available for the read command to extend its functionality. To see them, we can type help read, which will give us the manual for the read command. There are two options I would like to introduce– minus p, which allows a message to be displayed before the input is read, and minus s, which will hide the user input as they’re typing it. And this is useful for secrets like passwords. To demonstrate this, let’s create a new script called login.sh, in which we’ll ask the user to provide their username and password.
428.7
While the user is entering their password, we will be hiding the text they’re typing but storing their response as a variable in our script, which we can then either return back to the user or use later on in our script if we wanted to. Let’s take a closer look at the minus p option first, which is used in both of our read commands. When we were prompting for user input previously, the prompt and the user’s input were on two separate lines. When we use the minus p option, this allows the user input to be entered on the same line as they see the message or prompt. We can also look at the minus s option.
470.6
Here, we’re going to use this when we collect the password. The minus s option hides the user input as it’s being entered, so when we prompt asking them for their password, as the user is typing, the text that they are inputting won’t be shown in the terminal. But the text that they’ve entered isn’t lost. It will be stored in a variable in the background as part of our script. And we will then return it back to the user, just as an example. Let’s see how this is working in practise. As before, we’re going to use Nano in order to create and edit our script. This time, we’re calling it login.sh.
511.9
We’ll copy and paste our commands into that script, save it, and exit the editor. We’ll then update our permissions to make that script executable, and then we’ll execute our script, login.sh. First, you’ll see the prompt for your username. And you’ll be entering your response on the same line as that message. Next, you’ll see it prompt for your password. It looks like I’m doing nothing here, but while I’m typing, my input is hidden from the terminal. As you can see from the response, what I was typing was letmein as my password. This was stored as a variable, and then we’ve returned that back to you as an example in that message.

Watch Victoria presenting simple scripts demonstrating how to capture user input into a script

Here are the scripts used in this video:

whoami.sh

#!/usr/bin/env bashecho "Please enter your name."read nameecho "Hello ${name}, it’s nice to meet you!" food.sh #!/usr/bin/env bashecho "what are your two favourite foods?"read food1 food2echo "Your favourite foods are:${food1} and ${food2}" login.sh #!/usr/bin/env bashread -p "Enter your username: " usernameread -sp "Tell me your password: " passwordecho -e "nHi${username}, your password is \${password}"