Input in ShellScript: Reading User Input

In Bash scripting, the read command is a versatile tool that allows you to capture input from users. This can include single variables, multiple inputs, passwords, or even advanced options for flexibility.

Basic Syntax

The general syntax of the read command is:

read [options] [variable_names]
  • options: Flags to modify the behavior of the read command (e.g., silent input).
  • variable_names: The names of the variables where input data will be stored.

Examples

Reading a Single Value

Capture a single input value and display it back to the user:

#!/bin/bash
read name
echo "Hello, ${name}!"

Reading Multiple Values

You can read multiple variables at once:

#!/bin/bash
read name age
echo "Hello ${name}, you are ${age} years old."

Using a Prompt Message

Add a custom prompt message to make the input process more user-friendly:

#!/bin/bash
read -p "Please enter your name: " name
read -p "Please enter your age: " age
echo "Hello ${name}, you are ${age} years old."

Reading Secret Input

To securely capture sensitive data like passwords, use the -s option:

#!/bin/bash
read -p "Please enter your password: " -s password
echo
echo "Your password is stored securely."

Combining Prompt and Secret Input

#!/bin/bash
read -p "Enter your username: " username
read -p "Enter your password: " -s password
echo
echo "Welcome, ${username}!"

Advanced Usage

Specifying a Timeout

The -t option allows you to specify a timeout (in seconds) for user input:

#!/bin/bash
read -t 10 -p "Enter your name within 10 seconds: " name
echo "Hello, ${name}!"

Reading Default Values

Set a default value for a variable if no input is provided:

#!/bin/bash
read -p "Enter your name [Default: Gaurav]: " name
name=${name:-Gaurav}
echo "Hello, ${name}!"

Reading Input Into an Array

Use the -a option to read inputs into an array:

#!/bin/bash
read -p "Enter multiple values: " -a values
echo "You entered: ${values[@]}"

Handling Special Characters

Escape special characters during input using \ or quotes:

#!/bin/bash
read -p "Enter a command with special characters: " command
echo "You entered: ${command}"

Example Script

Here's a full script demonstrating multiple features of the read command:

#!/bin/bash
# Demo: Read Command

# Read single input
read -p "Enter your name: " name

# Read multiple inputs
read -p "Enter your age and city: " age city

# Read password securely
read -p "Enter your password: " -s password
echo

# Display results
echo "Name: ${name}"
echo "Age: ${age}, City: ${city}"
echo "Password: (hidden)"

Sample Output

When you run the script above, you’ll see:

┌──(gaurav㉿learning-ocean)-[~/shellscript-youtube]
└─$ ./read_script.sh
Enter your name: Gaurav
Enter your age and city: 30 New Delhi
Enter your password: 
Name: Gaurav
Age: 30, City: New Delhi
Password: (hidden)

Best Practices

  1. Always validate user input to avoid unexpected behavior or security vulnerabilities.
  2. Use prompt messages to guide users.
  3. Secure sensitive input (e.g., passwords) with the -s option.
  4. Provide defaults to ensure smooth script execution in case of empty input.
  5. Use arrays for structured data input.

Demo Video

Click Here for Demo Video