The simple case of the command is to display a variable. For example,
the following would display your browser's identification string:
<?echo $HTTP_USER_AGENT>
This looks like this:
The optional formatting string is especially handy for numerical
display. Assume for some reason we wanted to display floating
point numbers to different levels of precision. We'll assign the
result of the division 10.0/3.0 to the variable "a" with:
<?$a = 10.0 / 3.0>
$a = 10.0 / 3.0>
Note that 10.0 and 3.0 were used instead of 10 and 3. Without the .0's
the result would have been treated as an integer and not a floating
point value.
Now we will display $a with:
<?echo "%.2f" $a>
This looks like this:
The "%.2f" instructs echo to display $a as floating point number with 2 significant digits after the decimal place.
Now we will display $a with:
<?echo "%.9f" $a>
This looks like this:
See your Unix man pages, or any C language manual for the formatting arguments for the printf function for a complete list of all the formatting parameters available. The echo command supports all the same features and they are too exhaustive to cover completely here.