BASH Programming

How to Make a Shell Script (.sh) Wait for User Input – Bash

When creating a Bash script, it’s important to be able to prompt users for input. This can be done using the “read” command, but it can be tricky to use it in a way that makes your script pause and wait for the user’s response. This article will explore various methods for making a sh script wait for user input in Bash so if you are interested then go through this guide.

How to Use read Command to Make Bash Script Wait for User Input

In bash, you can make a .sh script wait for user input by using the read command. This command allows you to read user input from the terminal and store it in a variable. You can then use this variable to perform various actions in your script, here is an example bash script that uses this command:

#!/bin/bash

echo "Enter your name?"

read nm

echo "Greetings, $nm!"

When you run this script, it will prompt you to enter your name. Once you’ve entered it and pressed Enter, the script will print a greeting:

How to Use select Command to Make Bash Script Wait for User Input

Another way to make a .sh script wait for user input is by using the select command. The “select” command is another built-in command in bash that allows you to create a simple menu for the user to choose from, here is an example bash script that uses this command:

#!/bin/bash

weather_options=("sunny" "cloudy" "windy")
echo "Choose today's weather:"
select choice in "${weather_options[@]}"
do
  case $choice in
    "sunny")
      echo "You chose sunny."
      break
      ;;
    "cloudy")
      echo "You chose cloudy."
      break
      ;;
    "windy")
      echo "You chose windy."
      break
      ;;
    *)
      echo "Invalid option. Please choose a valid option."
      ;;
esac
done

In the script, we first define the “weather_options” array with three options: “sunny”, “cloudy”, and “windy”. Then we use the “echo” command to prompt the user to choose today’s weather. Next, we use the “select” command to display a numbered menu of the “weather_options” array and wait for the user to select an option.

The “case” statement is used to handle each possible user selection. If the user chooses “sunny”, “cloudy”, or “windy”, the script will display a message saying which option was chosen and then break out of the loop using the “break” statement. If the user enters an invalid option, the script will display a message saying that the option is invalid and prompt the user to choose a valid option.

Conclusion

Making a .sh script wait for user input is an essential part of creating interactive scripts. There are two ways to achieve this, including using the read command and select command. By using these methods, you can create powerful scripts that interact with the user and respond accordingly.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.