Build a Better Process

Linux read Command Summary with Examples

Linux read allows you to take input from a keyboard or file.
  1. Purpose - Learn what read is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with read.
  4. Script - Add read to our script and run it.
  5. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux read, 89 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / read-command.html

An ad-free and cookie-free website.

Examples of the Linux read Command


Learn to take input from a keyboard or file at the Linux command line.

Video Tutorial

Videos can also be accessed from the YouTube Linux Essentials Playlist on YouTube.

Linux read Command Summary with Examples (3:27)

Video Script

The Command and Why You Need It

Our eighty-ninth word, or command to memorize is read from our category Workflow.

read allows you to take input from a keyboard or file.

Common Linux read Options
-options description
-d DELIMITER Read until DELIMITER is input
-n NUMBER Limit input to NUMBER of characters
-t SECONDS Wait for input for SECONDS

Recall from videos (tutorials) #87 and #88, we're building a script to tie together what we've learned here, and now we'll take input from a keyboard.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The read command has 11 options and the argument is the name of a field, like a variable.

Unlike most commands, help is not available with double-dash --help, so look in the man page for bash, as with other built-ins. Option -d takes input until a delimiter is hit, like a comma or period, and -n waits for a number of characters and -t waits for seconds.

So why is read an important command? Well, you'll need input to build something interactive. And now you know how to do that.


Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. Recall we're adding lines to a script and I've been editing with nano from video (tutorial) #20, funscript located in ~/bin.

$ nano ~/bin/funscript

(Below is the screen from within nano.)

GNU nano 2.2.6 File: /home/factorpad/bin/funscript #!/bin/bash # The shebang gives the absolute path to the executable program # To see if it points to the right executable try: which bash # This is our first script called funscript # From video 87, print text and the current date/time echo -e "\nThe current date and time:" date # From video 88, we measure the time to run the script sleep 2s # From video 89, we take input from the keyboard echo -e "What is your name? \c" read yourname echo "Thank you $yourname" [ Read 20 lines ] ^G Get Help ^0 WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell

And I added these lines (the comment for video 89 and below). The \c here keeps input on the same line and the rest should be self-explanatory. Okay, please pause if you'd like to catch up and then we'll try it out.

(Hit Ctrl-x to leave nano and y to confirm saving.)

Now, run it by explicitly pointing to that directory and ./bin/funscript and here goes.

$ ./bin/funscript The current date and time: Mon Nov 15 09:50:33 PST 2016 What is your name? paul Thank you paul $ _

"What is your name?" Jeez, I know the answer to that, and there's the response.

So these are baby steps, granted, but at least we have a starting point.

And last, just to show you the -t timeout option, let's do a read -t 3 for three seconds.

$ read -t 3 $ _

(The system paused for 3 seconds before finishing and returning to the command prompt).

And this brings up data validation topics, like what if we input numbers, dates or reserved words, but we'll wait on that for now.

A Final Tip

Okay now you know how to use read. And you know the syntax for commands, options and arguments.

One last tip about the read command. So if you fail to assign a name to the input, read will store it in a variable called REPLY.

Okay, thanks for visiting today. I hope this was a helpful introduction to the read command.

Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:

What's Next?

Now that we're actually programming an interactive application, check out our YouTube Channel for related programming content.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / read-command.html

linux read command
bash read
shell script
linux shell scripting
bash read input
bash shell script
bash read file
read bash
bash input
bash keyboard input
bash read command
read linux
bash read line
bash script
read command
bash read file line by line
bash read command
linux read examples
bash read examples
shell scripting
linux read file

A newly-updated free resource. Connect and refer a friend today.