Home‎ > ‎CIS 98‎ > ‎

Input and Output

  1. The echo Command
    This is a function built into the shell, so there is no process overhead in executing it. There is a binary echo command in /bin in the event some dumb shell does not have that expected functionality.
    • Generic print statements in a shell script
    • Prompting users for input
    • Display contents of shell variables
    • Displaying error messages: command >&2
    Syntax: echo [-ne] [arg] ...
    echo escape sequences:
    • \b     Backspace
    • \f     Formfeed
    • \n     Newline
    • \r     Carriage return (no linefeed)
    • \t     Tab
    • \\     Backslash
    • \nnn   The character whose ASCII code is a 1,2 or 3-digit octal number

  2. The printf Command
    The bash shell offers another builtin command for outputing formatted strings which is not available in the Bourne shell.
    Syntax:
    printf [-v var] format [arguments]
    -v var
    By default, printf outputs to stdout, but if a variable name is supplied after the -v option, the ouptut will be assigned to the variable provided.
    format
    The format is a quoted string that defines how the arguments are to be formatted and displayed on the screen. It can be composed of three types of symbols:
    • plain character strings which are simply copied to stdout.
    • character escape sequences of the same type that the echo -e command supports.
    • format specifications, (e.g. %s, %d, %05d, ...) which correspond to the respective arguments. %s is for string format, %d is for numeric format, and a number in front of the letter indicates the field size.
    • You can find more format specifiers on the BASH hackers Wiki.
    arguments
    These are usually the values of variables that you want displayed, but they can also be numeric or string constants.

  3. The read Command
    The easiest way to get user input from the keyboard is to use the built-in read command.
    Syntax:
    read name
    read name1 name2 name3 ...
    The read command reads one line from the stdin and assigns values to the named user variables:
    • Each word on the command line is delimited from the next by "whitespace".
    • The first word is assigned to name1, the second to name2, and so on.
    • If there are more words than names, the extra words are assigned to the last name.
    • If there are fewer words than names, the extra names are assigned a null string.
  4. Debugging Techniques
    Like other commands, bash can take command line options to affect its behavior.
    1. The -v option
    2. The -x option
    3. The set command

Exercise 1: The echo command

What does the following display?
echo -e What is this?... "\0101"
What happens when the double quotes are removed?
 
Replace the following three lines with one echo statement that displays identical output:
echo Cabrillo College,
echo Aptos
echo California
Comments