How To

Bash Read Command: How To Read User Input In Scripts

Introduction

The Bash read command is used to take input from the user while a script is running. If you are learning Bash scripting, the read command is very important because it allows your script to interact with users.

Instead of writing fixed values inside a script, you can ask the user to enter a name, file path, username, password, option, or any other value. This makes Bash scripts more flexible and useful for automation, Linux administration, cybersecurity tasks, and menu-based tools.

What Is The Read Command In Bash?

The read command reads input from the terminal and stores it in a variable.

Basic syntax:

read variable_name

Example:

read nameecho "Hello, $name"

When the script runs, it waits for the user to type something. After the user presses Enter, the input is stored in the variable.

Basic Bash Read Command Example

Create a new Bash script:

nano read-example.sh

Add the following code:

#!/bin/bashecho "Enter your name:"read nameecho "Hello, $name"

Save the file and run it:

chmod +x read-example.sh./read-example.sh

Example output:

Enter your name:Kali UserHello, Kali User

Using Read With Prompt

You can use the -p option to show a prompt on the same line.

#!/bin/bashread -p "Enter your username: " usernameecho "Welcome, $username"

This is cleaner than using a separate echo command.

Reading Multiple Values

The read command can store multiple inputs in different variables.

#!/bin/bashread -p "Enter your first and last name: " first lastecho "First Name: $first"echo "Last Name: $last"

If the user enters:

Kali Linux

The script stores Kali in first and Linux in last.

Reading Password Input Silently

For password input, use the -s option. This hides the text while the user types.

#!/bin/bashread -s -p "Enter password: " passwordechoecho "Password received"

The extra echo moves the cursor to a new line after password input.

Cybersecurity Example: Simple Login Check

#!/bin/bashread -p "Username: " usernameread -s -p "Password: " passwordechoif [[ "$username" == "admin" && "$password" == "admin123" ]]; then    echo "Access granted"else    echo "Access denied"fi

This is a basic example for learning purposes. In real systems, avoid storing plain-text passwords inside scripts.

Conclusion

The Bash read command is used to collect user input and store it in variables. You can use it with prompts, multiple values, and silent password input.

For beginners, learning the read command is important because it helps create interactive Bash scripts. It is useful for automation, user input validation, menu scripts, Linux tools, and cybersecurity practice scripts.

Cyber Defence

Recent Posts

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

18 hours ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

18 hours ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

18 hours ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

18 hours ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

18 hours ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago