You are in: > Home > Developers Guides
Shell Scripting - Reading Variables From STDIN
To store a users input in a shell script use the 'read' command. This "Reads" the value of a variable from stdin, that is, it interactively fetches input from the keyboard.
An example of the use of read would is:
#!/bin/bash echo echo -n "What is your favorite vegetable? " read echo "Your favorite vegetable is $REPLY." # REPLY holds the value of last "read" if and only if # no variable supplied.
You will see that read stores it's result in a default variable called $REPLY. If you want to store the result in a variable with a name of your choice you simply add a variable name after the read command:
#!/bin/bash echo echo -n "What is your favorite fruit? " read fruit echo "Your favorite fruit is $fruit." # the variable $fruit absorbed the new "read" value.
There are other options you can supply to the read command, for the entire list type 'man read' at a command prompt, here are the most common options:
SYNTAX
read [-ers] [-a aname] [-p prompt] [-t timeout]
[-n nchars] [-d delim] [name...]
OPTIONS
-a aname
The words are assigned to sequential indices of the array variable aname,
starting at 0. All elements are removed from aname before the assignment.
Other name arguments are ignored.
-d delim
The first character of delim is used to terminate the input line,
rather than newline.
-e
If the standard input is coming from a terminal, Readline is used
to obtain the line.
-n nchars
read returns after reading nchars characters rather
than waiting for a complete line of input.
-p prompt
Display prompt, without a trailing newline, before attempting
to read any input. The prompt is displayed only if input is coming from a
terminal.
-r
If this option is given, backslash does not act as an escape character.
The backslash is considered to be part of the line. In particular, a backslash-newline
pair may not be used as a line continuation.
-s
Silent mode. If input is coming from a terminal, characters are not echoed.
-t timeout
Cause read to time out and return failure if a complete line
of input is not read within timeout seconds. This option has no
effect if read is not reading input from the terminal or a pipe.
Related articles
- Perl - Downloading a URL with a script - [22nd Mar 2006]
- Perl - Pattern Matching (Regular Expressions) - [12th Jan 2006]
- Shell Scripting - Reading Variables From STDIN - [11th Jan 2006]
- Perl - Configuring and Starting the mod_perl Server - [06th Jan 2006]
- Perl - Installing mod_perl in Three Steps - [06th Jan 2006]
- Perl - mod_perl - [06th Jan 2006]
- Perl - Associative Arrays - [06th Jan 2006]
- Perl - Manipulating Arrays - [06th Jan 2006]
- Perl - Arrays - [06th Jan 2006]
- Perl - Operators - [06th Jan 2006]
- Perl - Scalar Variables - [06th Jan 2006]
- Shell Scripting - Introduction - [08th Dec 2005]
- Shell Scripting - Invoking Shell Scripts - [08th Dec 2005]
- Shell Scripting - Variables - [08th Dec 2005]
- Shell Scripting - Referencing Variables - [08th Dec 2005]
- Shell Scripting - Command Arguments - [08th Dec 2005]
- Shell Scripting - Language Constructs - [08th Dec 2005]
- Perl - Introduction - [06th Dec 2005]
- MySQL - Connecting to the server - [06th Dec 2005]
- MySQL - Introduction - [06th Dec 2005]







