Printing strings to the screen ∞
printf like this:
printf "Hello, world!";
When you see
# =>, then the next line(s) would be the result of the earlier code.
I comment-out all of the output, so you can copy-and-paste the whole example without a problem.
printf "Hello, world!"; # => # Hello, world!
Strings require double-quotes.
- Single-quotes are not valid for strings!
Always end a one-line piece of code with a
The newline (
\n ) ∞
printf doesn't automatically send a newline (often interchangeably called a carriage return). So these are printed side-by-side:
printf "Hello, "; printf "world!"; # => # Hello, world!
To put them on two lines, you need to use
\n. Also, it's usually a good idea to put a
\n at the end of your text, so that any additional text that's printed afterwards will appear on its own line. :
printf "Hello, "; printf "\n"; printf "world!\n"; # => # Hello, # world!
Adding a newline at the end makes sure that any text that may come afterwards will begin on its own line. This is a good practice.
You can also add
\n inside one long string:
printf "Hello,\nworld!\n"; # => # Hello, # world!
The code to print text can get complex and ugly. Whenever possible, make a point to keep your code as readable as possible .. even if it's more typing!
Special characters ∞
' - Single-quotes ∞
Nothing special needs to be done to display a single-quote in a string.
printf "This works 'just fine'!\n"; # => # This works 'just fine'!
" - Double-quotes ∞
If you want to display a double-quote, then prepend a backslash (
\ ). A backslash "escapes" the character that follows it.
printf "Quotes \"like these\" are easy.\n"; # => # Quotes "like these" are easy.
\t for a tab character.
printf "\tHello, world!\n"; # => # Hello, world!
Note that the number of spaces the tab character becomes may vary from computer to computer. It depends on the configuration. Tabs usually stop at columns every eight characters.
\ - Literal backslash ∞
\\ for to display a backslash.
printf "One \\ two\n"; # => # One \ two
Long strings (
\ ) ∞
Imagine that we have a particularly long string. Perhaps your editor has it running off the right side of the screen, or perhaps your editor rudely word-wraps it. Code can be formatted to fit within an appropriate area, and so can text.
So imagine that we have this code:
printf "The red fox jumped over the lazy dog.\n The lazy dog wagged its tail.";
The obvious solution is to break it up into two
printf "The red fox jumped over the lazy dog.\n"; printf "The lazy dog wagged its tail.";
But what if that's not desirable?
We can continue a quoted string across physical newlines by using leading and trailing backslashes (
/ ) like so:
printf "The red fox jumped over the lazy dog.\n \ \The lazy dog wagged its tail."; # => # The red fox jumped over the lazy dog. # The lazy dog wagged its tail.
printf's more complex use ∞
printf can be used in another way. You've been using it like so:
printf "My string\n"; # => # My string
The other way is:
printf "%s" "My string\n"; # => # My string
%s means "string".
printf in this second manner is important for including different kinds of information in one string, without needing to convert everything into strings and concatenate them. More on that next..
Technically speaking, parentheses should be used:
printf "%s" ( "My string\n" );
.. and this will become mandatory for more advanced usage. For now, parentheses are optional.
Printing numbers to the screen ∞
%d - Decimal ∞
This is not valid code:
Only strings can be written to the screen. So this would be valid:
printf "1\n"; # => # 1
But you often want to keep your information in "number form". For that, you would use
printf another way.
Earlier we had this construct.
printf "%s\n" "My string";
%s for "string". The same thing applies for other types of values.
%d for "decimal". Those are whole numbers with no decimal point.
printf "%d\n" 1; # => # 1
printf "The number one: '%d'\n" 1; # => # The number one: '1'
%f - Floats ∞
%f for floating point numbers. Numbers with a decimal point.
printf "%f\n" 1.2; # => # 1.200000
You may get a different number of zeroes at the end. This will be
printf use ∞
Much more can be done, and is discussed in Mythryl Tutorial, printf.