Reading Input and Printing Output in Bash

By Anurag Singh

Updated on Dec 25, 2024

Reading Input and Printing Output in Bash

In this tutorial, we're reading input and printing output in bash.

Bash, or the Bourne Again Shell, is a widely used shell scripting language in Unix-based systems. Learning how to read input and print output is fundamental to scripting, as it allows interaction between the user and the script. We’ll explore how to use the read command to capture user input and the echo and printf commands to display output.

Prerequisites

Before getting started, ensure you have the following:

  • A KVM VPS or dedicated server with any Linux distro installed.
  • A non-root user with sudo privileges.
  • Basic knowledge of Linux commands.

Reading Input Using the read Command

The read command is a simple and powerful tool for taking user input in Bash scripts. It pauses the script and waits for the user to type something, which it then stores in a variable.

Syntax:

read [options] variable_name

Example 1: Basic Input

#!/bin/bash

# Prompt the user for their name
echo "Enter your name:"
read name

# Print a greeting
echo "Hello, $name!"

Explanation:

  • read name stores the user’s input in the variable name.
  • echo "Hello, $name!" prints the value of the name variable, demonstrating how user input can be used in the script.

Example 2: Input Without Prompt

#!/bin/bash

read -p "Enter your age: " age
echo "You are $age years old."

Here, the -p option lets you specify a prompt directly on the same line as the read command.

Example 3: Reading Multiple Variables

#!/bin/bash

read -p "Enter your first and last name: " first last
echo "First name: $first"
echo "Last name: $last"

When multiple variable names are specified, read splits the input by whitespace and assigns values to the variables sequentially.

Options for read:

  • -s: Silent mode (e.g., for passwords).
  • -t: Timeout after a specified number of seconds.
  • -n: Limit input to a specific number of characters.

Printing Output with echo

The echo command is the simplest way to display text or variable content to the terminal. It automatically adds a newline at the end of the output.

Syntax:

echo [options] [string]

Example 1: Basic Output

#!/bin/bash

echo "Hello, world!"

Example 2: Using Variables

#!/bin/bash

name="Alice"
echo "Welcome, $name!"

Example 3: Escaping Characters

#!/bin/bash

echo "This is a \"quoted\" word."

Options for echo:

  • -n: Prevents the trailing newline.
  • -e: Enables interpretation of escape sequences (e.g., \n for newline, \t for tab).

Printing Output with printf

The printf command provides more control over output formatting compared to echo. It follows the syntax and behavior of the C printf function.

Syntax:

printf format [arguments...]

Example 1: Basic Output

#!/bin/bash

printf "Hello, world!\n"

Unlike echo, printf does not append a newline automatically. You must explicitly include \n.

Example 2: Formatting Numbers

#!/bin/bash

num=42
printf "The answer is %d\n" $num

Example 3: Multiple Placeholders

#!/bin/bash

name="Bob"
age=30
printf "%s is %d years old.\n" $name $age

Format Specifiers:

  • %s: String.
  • %d: Decimal integer.
  • %f: Floating-point number.

Options for printf:

  • \t: Horizontal tab.
  • \n: Newline.
  • %.*f: Limits the number of decimal places.

Best Practices

  • Use read -p for concise prompts.
  • Favor printf when precise formatting is required.
  • Avoid hardcoding text; use variables to increase script flexibility.

By mastering read, echo, and printf, you can create interactive Bash scripts that engage with users effectively. These commands are fundamental tools that you’ll use in virtually every script you write.

The next topic in this series will cover Basic Conditional Statements. We will explore how to use if, elif, else, and case statements to add decision-making capabilities to your Bash scripts.