An ad-free and cookie-free website.
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
read allows you to take input from a
keyboard or file.
||Read until DELIMITER is input|
||Limit input to NUMBER of characters|
||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).
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
-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
nano from video
funscript located in
(Below is the screen from within
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.
Ctrl-x to leave
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
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
And you know the syntax for commands, options and arguments.
One last tip about the
command. So if you fail to assign a name to the input,
read will store it in a variable
Okay, thanks for visiting today. I hope this was a helpful
introduction to the
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.
timecommand, hit Back.
xargs, click Next.
A newly-updated free resource. Connect and refer a friend today.