Everyone wants their scripting to be more interactive, especially users. Users want to feel like they’re working with a modern piece of software instead of something cobbled together from old batch files and cryptic one-liners. They also want a self-documenting script that requires as little documentation as possible. If these things sound like what you’ve been waiting for, then read on! A shell script is a perfect place for user input because it is such a simple programming construct. It essentially consists of a series of commands that have been given their own names
The Linux read command provides you the option to prompt for user input. Once the user-provided input hits enter, the command store provided input to a variable. That can be used later in the shell scripts. In this tutorial, you will learn, how to prompt for user input in a shell script. Also, help you with accepting passwords from users in hidden characters.
Taking User Input in Bash
When writing a shell script, it may be helpful to prompt the user for input. This will allow you to get a specific value that you can use in your script as you see fit. It also allows you to make your script interactive by prompting the user to enter values such as directories and filenames.
In bash, you can prompt the user with a read command.
Here “read” is the Linux command and “my_var” is the variable, that will store the input value.
Hit enter after typing the above command. You will see a blank line without a prompt. It means the shell is waiting for your input. Type some text and hit enter button. You will find the prompt again.
Now, verify that the input value is stored in a defined variable. To confirm it, print the variable value with the echo command.
There is no need to define any data type for variables. Shell automatically adjusts the type based on the user input.
You can also add a message at the input prompt for user convenience. It looks like this:
read -p "What is your name? " my_var
In this example, the read command is followed by a
-p. This tells the script to prompt the user for input. The text inside the quotes after the -p is displayed to the user when they are prompted. By using read in your shell scripts, you can prompt the user for input and store the results in a variable. You can also use read to read in one value using a variable to prompt the user for another value. You can use read for many things, but it’s most useful for reading user input.
Shell Script to Prompt for User Input
Write a sample shell script to prompt the user input and store it in a variable. Make sure to print a proper message on the screen before prompting for input. Store the input value in a variable and then print it in with a welcome message.
Create a new file “input.sh” and write below script:
Save your file and close it.
chmod +x input.sh
In the above script, you found that the input is prompted in a new line. To make it better, use the
-p option to print the message before input with the read command. With this, we will skip the first echo line.
Edit your script and make the following changes.
Again execute the same script and see the outputs:
The output is self-explanatory. The inputed value is stored in variable and then prints it.
How to Input a Password in Shell Script
The password input should be hidden on the screen, similar to what the Linux shell does when setting a password on the screen. Passwords must not be visible on the screen.
The read command provides the silent mode option with “-s” to hide the input characters from the screen. Let’s create a sample script to take password input from the user.
Add the below script.
Save the file and close it.
chmod +x input-pass.sh
A shell script is a perfect place for user input because it is such a simple programming construct. It essentially consists of a series of commands that have been given their own names. This how-to guide helped you to understand the user input in shell scripts. Additionally provides you an example to take input of a password in the shell script.