Shell Scripting / Input and Output in Shell Scripts

Capturing User Input with read Command

This tutorial will introduce the 'read' command in shell scripts, which is used for capturing user input. You'll learn how to use it to create interactive scripts.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers capturing user input and redirecting output in shell scripts.

Capturing User Input with read Command: A Detailed Tutorial

1. Introduction

In this tutorial, we will explore the 'read' command used in shell scripts for capturing user input. This command helps in creating interactive scripts, leading to more user-friendly applications.

By the end of this tutorial, you will learn:
- How to use the 'read' command in shell scripts
- How to create interactive scripts using the 'read' command

Prerequisites:
- Basic understanding of shell scripting
- A Linux or Unix-like operating system

2. Step-by-Step Guide

The 'read' command in shell scripting is a built-in command that takes standard input (from the keyboard or file) and assigns it to a variable. This is handy for interactive scripts where user input is required.

Here's a simple usage of the read command:

echo "What is your name?"
read name
echo "Hello, $name"

In the above example, 'name' is a variable and 'read' command will assign the input from the user to this variable.

Best Practices and Tips

  • Always use clear and descriptive variable names to improve the readability of your script.
  • You can use the '-p' option with the read command to specify a prompt string. For example:
read -p "Enter your name: " name

3. Code Examples

Let's take a look at some practical examples.

Example 1: Prompting for Single Input

#!/bin/bash
# This is a simple script that asks for the user's name.

# Prompt the user for their name.
read -p "Please enter your name: " name

# Print a greeting message using the name provided.
echo "Hello, $name. Nice to meet you!"

When you run this script, it will prompt you to enter your name and then print a greeting message using the name you entered.

Example 2: Prompting for Multiple Inputs

#!/bin/bash
# This script asks for the user's first and last name.

# Prompt the user for their first and last names.
read -p "Please enter your first name: " firstname
read -p "Please enter your last name: " lastname

# Print a greeting message using the names provided.
echo "Hello, $firstname $lastname. Nice to meet you!"

This script asks for two separate inputs (first name and last name) and then uses both inputs in the greeting message.

4. Summary

In this tutorial, we learned about the 'read' command in shell scripting and how to use it to capture user input. We also went through some best practices and tips for using the 'read' command.

Next Steps:
- Try creating your own interactive scripts using the 'read' command.
- Learn about other built-in Bash commands and how they can be used in your scripts.

5. Practice Exercises

Now, it's time to test your knowledge. Here are some exercises to practice:

  1. Write a script that asks the user for their favorite color and then prints a message saying "Your favorite color is [color]."
  2. Write a script that asks the user for their name and age and then prints a message saying "Hello, [name]. You are [age] years old."

Solutions:

  1. Favorite Color Script
#!/bin/bash
read -p "What's your favorite color? " color
echo "Your favorite color is $color."
  1. Name and Age Script
#!/bin/bash
read -p "What's your name? " name
read -p "How old are you? " age
echo "Hello, $name. You are $age years old."

Keep practicing and exploring more features of shell scripting. Happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Scientific Calculator

Perform advanced math operations.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

CSV to JSON Converter

Convert CSV files to JSON format and vice versa.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help