← Back to Bash Course | Chapter 3: Control Flow | Lesson 1 of 14

The read Command

In this page:

  1. The read Command

The read Command

read pauses a script and waits for the user to type input, storing it into a variable. read -p "prompt" displays a prompt before waiting. It's the standard way to make an interactive script that responds to user input.

Note: Use read -s to read input silently, such as when prompting for a password.

Example: The read Command

bash
#!/bin/bash
echo "Simulating input via echo pipe since this runs non-interactively:"
name="Guest"
echo "Hello, $name! (in an interactive terminal, 'read -p \"Name: \" name' would prompt you)"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.