The Korn Shell print Statement Examples
Use the print statement to provide output. Use it wherever you would use the echo statement. It is more versatile than the echo statement. The print statement has several options:
- -n - Suppresses the newline character after printing the message. This usually is used when printing a prompt for user input.
- -r - Turns off the special meaning of the \ character.
- -R - Does not interpret the - that follows as an option to the print statement, except if -R is followed by n; that is, if the -n option follows -R, -n is still taken as an option. The -R option is useful if you need to print negative numbers.
- – - Same as -R, except that a following -n option is taken literally.
The print statement also interprets the following as special characters when they are enclosed in double quotes:
- \n - Prints a newline character, which enables you to print a messageon several lines using one print statement.
- \t - Prints a tab character, which is useful when creatingtables or a report.
- \a - Rings the bell on the terminal, which draws the attentionof the user.
- \b - Specifies a backspace character, which overwrites the previous character.
Note: Provide the special characters to the print statement within double quotes. The statements print \n and print “\n” do not give the same results.
Examples of Using the print Statement
The following are examples of the print options and special characters displayed on the command line. Experiment with these to see how they work.
$ print "Hello there.\nHow are you?"
Hello there.
How are you?
$ print -r "Hello there.\nHow are you?"
Hello there.
\nHow are you?”
$ print "-2 was the temperature this morning."
ksh: print: bad option(s)
$ print -R "-2 was the temperature this morning."
-2 was the temperature this morning.
$ print -- "-2 was the temperature this morning."
-2 was the temperature this morning.
$ print -- -n "is the option."
-n is the option.
$ print -R -n "is the option."
is the option.$
$ print -n "No newline printed here. "
No newline printed here. $
$ print "Hello\tout\tthere!"
Hello out there!
$ print "\aListen to me!"
Listen to me!
$ print "Overwrite\b the 'e' in 'Overwrite'."
Overwrit the 'e' in 'Overwrite'.