read

shell builtinLinux/Unix
The read command is one of the most frequently used commands in Linux/Unix-like operating systems. read Read a line from standard input or a file descriptor

Quick Reference

Command Name:

read

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

read [options] [arguments]

Common Use Cases

    Syntax

    read [options] [variable...]

    Options

    Option Description
    -a array Read words into the specified array variable, starting at index 0
    -d delim Read until the first character of DELIM instead of newline
    -e Use Readline to obtain the line (enables command history and editing)
    -i text Use TEXT as the initial text for Readline
    -n nchars Read exactly NCHARS characters, if possible
    -N nchars Read exactly NCHARS characters, ignoring delimiter
    -p prompt Output the string PROMPT without a trailing newline before reading
    -r Backslash does not act as an escape character
    -s Do not echo input (useful for passwords)
    -t timeout Time out and return failure if a complete line is not read within TIMEOUT seconds
    -u fd Read from file descriptor FD instead of standard input

    Examples

    How to Use These Examples

    The examples below show common ways to use the read command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    read name
    Read a line of input into the variable 'name'.
    read first last
    Read a line of input and split it into variables 'first' and 'last'.
    # Advanced Examples Advanced
    read -p "Enter your name: " username Display a prompt and read input into 'username' variable. read -s -p "Enter password: " password Read a password securely (without displaying input). read -n 5 chars Read exactly 5 characters into the 'chars' variable. read -t 10 response Wait 10 seconds for input, then continue. read -a array_name Read input into an array. read -r line Read a line without interpreting backslash escapes. while read line; do echo "Processing: $line" done < input.txt Read a file line by line in a loop. IFS=":" read user pass uid gid desc home shell < /etc/passwd Read fields from a colon-delimited file into separate variables.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `read` command is a fundamental shell built-in that reads a line of input from standard input or a specified file descriptor and assigns the input to one or more variables. It's one of the primary ways to create interactive shell scripts that accept user input. As a built-in command in bash and other Unix shells, `read` doesn't create a separate process, making it efficient for capturing input. The command waits for the user to type a line of text, followed by the Enter key, and then assigns the input to the specified variables. Key features and uses of the `read` command include: 1. Interactive Input: Collect information from users in shell scripts, such as names, preferences, or confirmation prompts. 2. File Processing: Read files line by line for processing in scripts, a common pattern for text file manipulation. 3. Input Validation: Combined with conditional statements, `read` can validate user input before proceeding with operations. 4. Secure Password Entry: With the `-s` option, passwords or sensitive information can be collected without displaying the characters on screen. 5. Timed Input: The `-t` option allows scripts to continue after a timeout, preventing indefinite waiting for user input. 6. Field Splitting: Input can be automatically split into multiple variables based on the Internal Field Separator (IFS) value. 7. Array Population: Input can be read directly into an array for more complex data handling. The `read` command is particularly powerful when combined with the `IFS` (Internal Field Separator) variable, which determines how input is split into fields. By default, IFS includes spaces, tabs, and newlines, but it can be customized to parse different types of delimited data. Common patterns for using `read` include: - Simple prompts: `read -p "Enter your name: " name` - Password input: `read -s -p "Password: " password` - Processing files: `while read line; do ... done < file.txt` - Reading fixed-length data: `read -n 4 code` - Reading into arrays: `read -a my_array` - Custom field separation: `IFS=":" read user pass uid gid < /etc/passwd` The `read` command's ability to handle various input types and formats makes it an essential tool for shell scripting, particularly for creating interactive utilities and processing text data.

    Related Commands

    These commands are frequently used alongside read or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the read command works in different scenarios.

    $ read
    View All Commands