/ factorpad.com / tech / linux-essentials / read-command.html
An ad-free and cookie-free website.
Intermediate
Learn to take input from a keyboard or file at the Linux command line.
Videos can also be accessed from the YouTube Linux Essentials Playlist on YouTube.
Linux read Command Summary with Examples (3:27)
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.
-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
.
(Below is the screen from within nano
.)
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.
"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.
(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.
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.
For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:
Now that we're actually programming an interactive application, check out our YouTube Channel for related programming content.
time
command,
hit Back.xargs
, click Next./ factorpad.com / tech / linux-essentials / read-command.html
A newly-updated free resource. Connect and refer a friend today.