read command in Linux with Examples
read command in the Linux system is used to read from a file descriptor. This command reads up the total number of bytes from the specified file descriptor into the buffer. If the number or count is zero, this command may detect errors. But on success, it returns the number of bytes read. Zero indicates the end of the file. If some errors are found then it returns -1.
Let’s look deeper into its usage, syntax, options, and some common examples.
Syntax
read
The read command takes the user's input and stores it into a variable that can be referenced later in the script.
Basic read Command Example


Options for the read Command
Options | Description |
---|---|
-p | Displays a prompt before reading input. |
-t | Sets a timeout to wait for input. |
-n | Limits the number of characters to read. |
-s | Disables echoing the input (useful for password entry). |
-d | Defines a delimiter other than a newline. |
read Command Examples in Linux
read command without any option: The read command asks for the user's input and exit once the user provides some input.
In the following example we are acquiring the user's name and then showing the user's name with a greeting.
echo "what is your name..?";read name;echo "hello $name"

Conclusion
The read command's ability to capture user input, read files, and handle input with various options makes it essential for interactive and automated scripts. By mastering this command and its various flags, you can make your shell scripts more dynamic and user-friendly.