9.1. Writing files

echo

So far we have used echo to print text into the terminal. You can also use this utility to print text into a file (and create a new file if it doesn't exist):

The following command will append "some text" in a new line of someFile.txt and add the new line character at the end. If someFile.txt didn’t exist, the file is created.
echo "some text" >> someFile.txt

If you add the flag -n, bash won't print the trailing newline character:
echo -n "some text" >> someFile.txt

If you add the flag -e it will interpret the character scape sequences in the text (see table below in the printf section for a list of scape sequences).

If you use > instead of >>, the previous contents of the file (if it existed) will be erased and replaced with the new text that you are echoing.

printf

printf is a powerful tool that allows you to format the information before printing it in a file, the command line or another variable. For example, you can specify the format of any number that you print and the number of decimal points you want to use. You could even use this tool to change the format of a variable (i.e. from scientific notation to float) and save the result in a new variable instead of a file. You can also add tab or any character scape sequence to your text.

Syntax: printf <format> <arguments>

printf uses the format specified in <format> to print the objects (strings, numbers or variables) specified in <arguments>. <format> is a string that contains alphanumerical characters, character scape sequences and format specifications, each of which causes printing of the next successive argument. In contrast with the echo command, printf does not print the text in a new line by default, in order to add a new line the following character scape sequence should be added at the end of <format>: \n.

Character scape sequence Meaning
\b

Do not print the previous character (backspace).

$ printf "%b" "abcdef"
abcdef

$ printf "%b" "abc\bdef"
abdef

\c
\n

Write a new-line character.

$ printf "%b" "abc\ndef"
abc
def

\r Write a <carriage return> character.
\t

Write a <tab> character.

$ printf "%b" "abc\tdef"
abc def

\v

Write a <vertical tab> character.

$ printf "%b" "abc\vdef"
abc
def

\' Write a single quote character.
\\ Write a backslash character.
\num Write a byte whose value is the 1-, 2-, or 3-digit octal number num. Multibyte characters can be constructed using multiple \num sequences.

Each format specification is introduced by the percent character (%), followed by the following fields (in the exact order). You must use one format specification for each argument (in the same order):

Flag Meaning
#

When printing an octal number: Inserts a 0 prefix.

(234)10 = (352)8

$ printf "%o\n%#o\n" 234 234
352
0352

When printing a hexadecimal number: Inserts a 0x prefix.

Given that (234)10 = (EA)16

$ printf "%x\n%#x\n" 234 234
ea
0xea

When printing numbers using formats e (E), f (F) or g (G): Show the decimal point.

- Left justify.
+ Place a sign before the number when using signed formats.
<blank space&ht; Positive values begin with a blank.
0 Field is padded with zeros instead of blanks.

Field width or precision: An integer number preceded by a period (.) or an asterisk (*). If a number is given, it will represent the number of characters or digits to print if the argument is a string or a number (respectively). If an asterisk is given instead of a number, the field width or precision will be specified as another argument.

$ printf "%s\n" "qwertyuiop"
qwertyuiop

$ printf "%.4s\n" "qwertyuiop"
qwer

$ printf "%.*s\n" 6 "qwertyuiop"
qwerty

Format: This is a character that indicates the type of format that should be used to print the arguments. The character options are listed in the following table.

Character Format
%

Prints the symbol %. No argument is used

$ printf "%%"
%

$ printf "%% %s" "Some string"
% Some string

a or A Prints the argument in floating-point, hexadecimal form. The style used is [-h.hhh+-pd]. Most probably you will never need to use this format other than for technical purposes.
b

Same as s, but interprets the character scape sequences instead of reading them as literal strings.

$ printf "%s" "text\ntext"
text\ntext

$ printf "%b" "text\ntext"
text
text

c

The first byte of the argument is printed. Which will correspond to the first character if the argument is a string, or the first digit if it is a number.

$ printf "%c %c" "some string" 199
s 1

d or i

The argument must be a positive or negative integer. If no precision is specified, it just prints the number, otherwise it adds zeros before the integer to achieve the number of digits specified in the precision.

$ printf "%d\n%i\n" 2 -2
2
-2

$ printf "%.3d\n%.5i\n" -2 2
-002
00002

e or E

Writes the number in the argument in scientific notation, with format [-]d.ddde+-dd. There will be one digit before the decimal point and six digits after the decimal point if no precision is specified (or the number of digits specified in the precision). Infinity is printed as inf and NaN as nan. If the flag is in upper case, the e of the output will be printed in capital letter.

234.567 equals 2.34567 × 102 in scientific notation. So, printf would print that number in the following way:

$ printf "%e" 234.567
2.345670e+02

$ printf "%.1e" 234.567
2.3e+02

$ printf "%.1E" 234.567
2.3E+02

f or F

Prints the argument as a floating-point number with the format [-]ddd.ddd. The number of digits after the decimal point equals the precision specification for the argument or six digits if no precision was specified. Infinity is printed as inf and NaN as nan.

$ printf "%f\n" 2.34567890123
2.345679

$ printf "%.3f\n" -2.34567890123
-2.346

g or G

The argument is printed in style f (F) or in style e (E) whichever gives full precision in minimum space.

$ printf "%g\n%g\n" 238.567 0.000001
238.567
1e-06

o

The argument must be a positive integer. Prints the octal value of the argument. it adds zeros before the octal to achieve the number of digits specified in the precision.

Given that (234)10 = (352)8

$ printf "%o\n%.4o\n" 234 234
352
0352

s

Prints the string specified in the argument. It will stop when the number of characters specified in the precision is reached or at the end of the string if the precision is not specified (or if the string has less characters than the precision).

$ printf "%s" "example"
example

$ printf "%.3s" "example"
exa

$ printf "%.10s" "example"
example

u

The argument must be a positive integer. If no precision is specified, it just prints the number, otherwise it adds zeros before the integer to achieve the number of digits specified in the precision.

$ printf "%u\n%.5u\n" 2 2
2
00002

x or X

The argument should be a positive integer. Prints the hexadecimal value of the argument. If any precision is specified, it adds zeros before the hexadecimal to achieve the number of digits specified in the precision.

Given that (234)10 = (EA)16

$ printf "%x\n%X\n%.4x\n" 234 234 234
ea
EA
00ea

You can save the output of printf into a variable instead of printing it. For example, if you have a number in scientific notation and you want to convert it to floating, you can type the following:

$ FLOAT=$(printf "%f" 2.345670e+02)
$ echo $FLOAT
234.567000