01. Standard Outputting echo, printf

The echo command is an easy way of outputting data to a user.

$ echo 'Hello world!'
Hello world!
$ echo $PS1
\[\033[1;34m\]\W$\[\033[0m\]

This command is extremely useful, but primitive. Can we do better?

We could use echo with the option -e to use escape sequences.

\a
alert
\b
backspace
\c
suppress new line
\n
new line
\r
carriage return
\t
horizontal tab
\\
backslash

Can we do even better, making sure our code is 100% portable and POSIX standard? Yes, we can with printf.

Fancy printing with printf

In order to make a more flexible command that complies with POSIX standards, the printf command was adopted.

Although certainly more flexible, the printf command forces the user to be more explicit. For example, the newline character is not defaulted like it is in echo - we must supply it on our own.

$ printf "Hello world!\n"
Hello world!

Format specifiers

Just like printf is used in C and many other compiled languages, we may supply it a variable proceeding the declaration. For example:

$ printf "Hello my name is %s %s.\n" Tony Dylan
Hello my name is Tony Dylan.

The %s is a variable that stands in for strings, while the %d stands in for integers.

$ printf "I am %d years old" 44
I am 44 years old
$ printf "I am %d years old" 0x2c
I am 44 years old

To print floating point values, use the %f specifier.

$ printf "The child is %f years old.\n" 3.5
The child is 3.5 years old.

Formatting

To limit decimal places, we can place a decimal point and the precision within the floating specifier.

$ printf "%.2f\n" 3
3.00

To place spaces before a numerical value, we can do so with just an integer value.

$ printf "I am %4d years old.\n" 13
I am   13 years old.

To pad a number with zeros, precede with a zero.

$ printf "I am %04d years old.\n" 13
I am 0013 years old.

Treating "anomalies"

If there are fewer stand-ins than variables supplied, then the shell repeats the line until done.

$ printf "I ate some %s.\n" eggs chicken bacon
I ate some eggs. 
I ate some chicken. 
I ate some bacon.

On the other hand, if there are more stand-ins than variables, then the shell simply omits them.

$ printf "I ate some %s, %s and %s.\n" eggs chicken
I ate some eggs, chicken and .

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Self-Massage Tool

Aching back from coding all day? Try Back Problems

Relieve spasms, tight muscles, trigger points and pressure points with the Body Back Buddy! This trigger point massage is designed to help you self-message any area of your body - especially those that are hard to reach. Keeping your muscles relaxes and out of contraction is importan in helping to reduce pain and prevent muscle injury.

$ Check price
29.9529.95Amazon 4.5 logo(3,443+ reviews)

More Back Problems resources

Ad