read Command in Linux

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Overview

The read command in Linux is an in-built command-line utility that is primarily used for accepting user input or reading a line of input from a standard input (stdin). Its functionality doesn't stop there though, with a range of flags to add to its utility, from controlling the input delimiter to setting a timeout for the input. It is widely used in shell scripts for taking user input during the execution of the script.

Syntax of read Command in Linux

The basic syntax of the read command in Linux is as follows:

Where:

  • options: These are flags that alter the behaviour of the read command. Some of the most commonly used options include '-p' for displaying a prompt, '-t' for setting a timeout, and '-a' for reading into an array.
  • name: The variables where the input from the read command will be stored. If more than one variable name is given, the line of input is split into words and each word is assigned to a variable.
Sharpen Your Fundamentals with Free Learning

Options in read Command in Linux

  1. -p: Displays a prompt to the user before reading the input.

    For example -

    Output:

    This command prompts the user to enter their name and stores the input in the 'name' variable.

  2. -t: Sets a timeout for the read command. If no input is provided within the specified number of seconds, the command ends.

    For example -

    Output:

    This command waits for 5 seconds for the user to enter their name. If no input is provided within 5 seconds, the command ends.

  3. -a: Reads the input into an array instead of a single variable.

    For example -

    This command reads the input into an array named 'names'.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example Usages

  • Basic usage of the read command:

    Explanation: This command waits for the user to input a line of text, which is then stored in the 'name' variable.

  • Reading multiple inputs into separate variables:

    Explanation: The command waits for the user to input two words, which are then stored in the 'firstName' and 'lastName' variables respectively.

Tips

  • The read command in Linux is not limited to taking user input, it can also be used to read file content line by line in shell scripting.

  • The IFS (Internal Field Separator) variable is crucial when reading multiple inputs, as it determines how the input is split into separate variables.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Advanced Use Cases of read Command in Linux

  • Reading input with a prompt and a timeout:

    Output:

    Explanation: The command prompts the user to enter their name within 5 seconds. If no input is provided, the command ends.

  • Reading input into an array:

    Explanation: The command reads the user input into an array named 'colors'. Each word of input becomes an element of the array.

  • Reading from a file using the read command in a loop:

    Explanation: This command reads the content of 'file.txt' line by line and prints each line. The '-r' option prevents backslashes from being interpreted as escape characters.

Conclusion

  • The read command in Linux is a powerful tool for handling user input and reading file content.

  • Its utility can be expanded with options, allowing it to set a timeout for input, display a prompt, and read input into an array among other things.

  • The read command's ability to integrate with other commands makes it a cornerstone in shell scripting.