Skip to main content
ubuntuask.com

Back to all posts

How to Read User Input In Bash?

Published on
4 min read
How to Read User Input In Bash? image

Best Bash Scripting Guides to Buy in October 2025

1 Black Hat Bash: Creative Scripting for Hackers and Pentesters

Black Hat Bash: Creative Scripting for Hackers and Pentesters

BUY & SAVE
$40.41 $59.99
Save 33%
Black Hat Bash: Creative Scripting for Hackers and Pentesters
2 Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))

  • QUALITY ASSURANCE: CAREFULLY INSPECTED FOR READABILITY AND CONDITION.
  • ECO-FRIENDLY CHOICE: SUPPORTS SUSTAINABILITY BY REUSING BOOKS.
  • BUDGET-FRIENDLY: SAVE MONEY WITH AFFORDABLE PRICES ON QUALITY READS.
BUY & SAVE
$22.15 $44.99
Save 51%
Learning the bash Shell: Unix Shell Programming (In a Nutshell (O'Reilly))
3 The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting

BUY & SAVE
$27.89 $49.99
Save 44%
The Ultimate Linux Shell Scripting Guide: Automate, Optimize, and Empower tasks with Linux Shell Scripting
4 Classic Shell Scripting

Classic Shell Scripting

BUY & SAVE
$25.73 $49.99
Save 49%
Classic Shell Scripting
5 The Linux Command Line, 2nd Edition: A Complete Introduction

The Linux Command Line, 2nd Edition: A Complete Introduction

BUY & SAVE
$19.58 $39.99
Save 51%
The Linux Command Line, 2nd Edition: A Complete Introduction
6 Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)

BUY & SAVE
$36.76 $49.95
Save 26%
Scripting: Automation with Bash, PowerShell, and Python—Automate Everyday IT Tasks from Backups to Web Scraping in Just a Few Lines of Code (Rheinwerk Computing)
7 The Book of Batch Scripting: From Fundamentals to Advanced Automation

The Book of Batch Scripting: From Fundamentals to Advanced Automation

BUY & SAVE
$44.59 $59.99
Save 26%
The Book of Batch Scripting: From Fundamentals to Advanced Automation
8 Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali

BUY & SAVE
$23.74 $39.99
Save 41%
Linux Basics for Hackers: Getting Started with Networking, Scripting, and Security in Kali
9 Bash Pocket Reference: Help for Power Users and Sys Admins

Bash Pocket Reference: Help for Power Users and Sys Admins

BUY & SAVE
$17.60 $24.99
Save 30%
Bash Pocket Reference: Help for Power Users and Sys Admins
10 Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming

BUY & SAVE
$2.99
Shell Scripting: How to Automate Command Line Tasks Using Bash Scripting and Shell Programming
+
ONE MORE?

You can read user input in Bash using the read command. The syntax is as follows:

read [OPTIONS] [VARIABLE]

Here, OPTIONS are the different options or flags that can be used with the read command, and VARIABLE is the name of the variable that will store the user input.

Some commonly used options with the read command are:

  • -p "message": Displays a message to the user before reading input.
  • -s: Makes the input silent, so it won't be displayed as the user types.
  • -n: Specifies the number of characters that should be read.
  • -r: Used to preserve the line's backslashes instead of treating them as escape characters.

To read user input, you can simply write:

read VARIABLE

For example, to read a name from the user, you can use:

read name

The user can then type their name and press Enter. The value entered will be stored in the name variable.

You can also display a message to the user before reading input. For instance:

read -p "Enter your age: " age

This will display the message "Enter your age: " and store the entered value in the age variable.

Additionally, if you want to read a single character from the user without displaying it on the terminal, you can make use of the -s and -n options:

read -s -n 1 key

This will read a single character from the user and store it in the key variable without displaying it on the terminal.

These are some of the basic ways to read user input in Bash. You can customize the options and message based on your requirements.

How to read user input without displaying it on the screen in Bash?

In Bash, you can read user input without displaying it on the screen by using the -s option of the read command. This option is used to make the input silent.

Here's an example:

#!/bin/bash

echo "Enter your password: " read -s password

echo "Your password is: $password"

In this example, the -s option is used with read to make the user input silent. When the user enters their password, it won't be displayed on the screen. After the input is stored in the variable password, you can use it for further processing without displaying it explicitly.

What is the use of the -p option in the read command?

The -p option is used with the read command in shell scripting to display a prompt to the user while reading input.

For example, consider the following code snippet:

read -p "Enter your name: " name echo "Hello, $name!"

When the script is executed, it will display Enter your name: as the prompt, allowing the user to enter their name. After the user enters their name and presses Enter, the script will display Hello, [name]!, where [name] is the value entered by the user.

By using the -p option, the read command combines the prompt message with the input statement on one line, providing a clear expectation of what input is required.

How to prompt the user for input in Bash?

To prompt the user for input in a Bash script, you can use the read command. Here's an example:

#!/bin/bash

Prompt the user for their name

echo "Enter your name:" read name

Display a greeting message

echo "Hello, $name!"

In this example, the read command is used to read the user's input into the name variable. The user is prompted with the message "Enter your name:". After the user enters their name and presses Enter, the input is stored in the name variable. Finally, the script displays a greeting message using the entered name.

You can customize the prompt message by modifying the string passed to the echo command before the read command.