Input/Output
Echo
To "print" data from a shell script, you use the echo command.
echo
takes the string that follows it (including variables) and sends the result to standard output (usually the screen). To make your life easier,echo
also prints a line feed following its output. Sometimes, however, it is less than desirable to move down a line - especially when you are prompting the user for input. To preventecho
from moving down a line, invoke it with the-n
option. For example, to print the contents of the variable name and without a line feed:echo -n "Your name is $name"
cat and Input redirection
If you happen to have a great deal of text that you want to print out (instructions for using the shell script, for example), it might be a bit tedious to use echo repeatedly to get the job done. Instead, you can use the
cat
command to redirect text between 2 points to standard output as shown here in this example:This code fragment would print lines of text between the line containing cat and the arbitrarily defined tag
cat << STOPPINGPOINT this is a line of text. This is another line. STOPPINGPOINTSTOPPINGPOINT
. Any variables contained in the lines will also be evaluated. This is a useful shortcut if you have to include large amounts of textual output in your program.read
The read command lets you read information from standard input and store it in a variable. This enables you to prompt the user for information and act on that information.
For example, to read data into the variable name, you would use the following:If you'd like to break the input up into multiple parts by word , that's just as simple as reading into multiple variables. For instance, if I wanted the user to enter her city state and zip all on 1 line, I could read the information separately with a command like this:read name
This would divide the input up by words and store the first word in city, the second in state and the third in zip. If there were additional words on the input line, they would be stored in the last referenced variable (in this case zip).read city state zip
If you want to read passwords, but don't want them to be displayed, the following code will help:
echo -n "Password: " stty -echo read PASSWORD stty echoArguments to a command
Most Linux/Unix utilities, even those that have an interactive prompting mode, offer support for feeding the program the important parameters via the command line. For example, you can pass date to the program doit by adding the parameters that you want to pass to it at the end of the command line. Typically, these parameters are separated by spaces. If you'd like to use a space in one of the parameters, enclose that parameters in quotes.
Example:2 parameters are passed. The first parameter is "John Do", while the second is a number, 43511. Since the first has a space in it, the quotes are necessary. In order to access these parameters from within a bash script, you use the special positional variables: $1, $2, .... The first parameter is in $1 the second in $2, and so on. The name of the command that was invoked is $0. You can also reference all the parameters through the special $*.doit "John Do" 43511
Reading the results of Another command
There is another interesting way that you can get data into a script that you're writing. If, for example, you have a command that prints out the result of the date command, you can store the output in a variable by using backtick quotes(``). Let's say you want to store the output of date in the variable timeanddate, you could do this:
timeandday=`date`