Linux Commands – printf
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed echo command in Linux which is used to print in standard output in Linux.
https://cloudaffaire.com/linux-commands-echo/
In this blog post, we will discuss printf command in Linux. printf command is used to print a formatted string in the standard output while interpreting ‘%’ directives and ‘\’ escapes to format numeric and string arguments in a way that is mostly similar to the C ‘printf’ function.
Format-Control Letters:
A format specifier starts with the character ‘%’ and ends with a format-control letter—it tells the printf statement how to output one item. The format-control letter specifies what kind of value to print. The rest of the format specifier is made up of optional modifiers that control how to print the value, such as the field width. Here is a list of the format-control letters.
Format | Details |
%a | A floating point number of the form [-]0xh.hhhhp+-dd (C99 format). |
%A | Same as %a, only prints uppercase instead of lowercase letters. |
%b | Print the associated argument while interpreting backslash escapes in there |
%c | Print a number as a character; thus, ‘printf “%c”, 65’ outputs the letter ‘A’. The output for a string value is the first character of the string. |
%d | Print a decimal integer. |
%i | Same as %d, for compatibility with ISO C. |
%e | Print a number in scientific (exponential) notation. |
%E | Same as %e, only prints uppercase instead of lowercase letters. |
%f | Print a number in floating-point notation. |
%F | Same as %f, but the infinity and “not a number” values are spelled using uppercase letters. |
%g | Print a number in either scientific notation or in floating-point notation, whichever uses fewer characters. |
%G | Same as %g, only uses uses ‘E’ instead of ‘e’. |
%n | Assigns the number of characters printed so far to the variable named in the corresponding argument. |
%o | Print an unsigned octal integer. |
%q | Print the associated argument shell-quoted, reusable as input |
%s | Print a string. |
%u | Print an unsigned decimal integer. |
%x | Print an unsigned hexadecimal integer. |
%X | Same as %x, only uses the letters ‘A’ through ‘F’ instead of ‘a’ through ‘f’. |
%% | Print a single ‘%’. |
Modifiers for printf Formats:
A format specification can also include modifiers that can control how much of the item’s value is printed, as well as how much space it gets. The modifiers come between the ‘%’ and the format-control letter. Here are the possible modifiers, in the order in which they may appear.
Modifiers | Details |
N$ | An integer constant followed by a ‘$’ is a positional specifier. |
– | The minus sign, used before the width modifier, says to left-justify the argument within its specified width. Normally, the argument is printed right-justified in the specified width. |
space | For numeric conversions, prefix positive values with a space and negative values with a minus sign. |
+ | The plus sign, used before the width modifier, says to always supply a sign for numeric conversions, even if the data to format is positive. The ‘+’ overrides the space modifier. |
# | Use an “alternative form” for certain control letters. For ‘%o’, supply a leading zero. For ‘%x’ and ‘%X’, supply a leading ‘0x’ or ‘0X’ for a nonzero result. For ‘%e’, ‘%E’, ‘%f’, and ‘%F’, the result always contains a decimal point. For ‘%g’ and ‘%G’, trailing zeros are not removed from the result. |
0 | A leading ‘0’ (zero) acts as a flag indicating that output should be padded with zeros instead of spaces. This applies only to the numeric output formats. This flag only has an effect when the field width is wider than the value to print. |
‘ | A single quote or apostrophe character is a POSIX extension to ISO C. It indicates that the integer part of a floating-point value, or the entire part of an integer decimal value, should have a thousands-separator character in it. This only works in locales that support such characters. |
Linux Commands – printf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
############################# ## Linux Commands | printf ## ############################# ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------- ## printf ##------- ## printf [format]... [arguments]... ## printf [-v var] [format]... [arguments]... ## string formatting printf hello world ## returns hello printf "hello world" ## returns hello world printf 'hello world' ## returns hello world printf "%s" "hello world" ## returns hello world printf "%s - %s" "hello" "world" ## returns hello - world printf "%s\t%s" "hello" "world" ## returns hello world printf "hello %s welcome to %s" "debjeet" "cloudaffaire" ## returns hello debjeet welcome to cloudaffaire printf "%c" "hello" ## returns h ## number formatting printf "%d" 10 ## returns 10, 10 in signed decimal printf "%i" 10 ## returns 10 printf "%d" -10 ## returns -10, -10 in signed decimal printf "%u" -10 ## returns 18446744073709551606, -10 in unsigned decimal printf "%o" 10 ## returns 12, 10 in octal printf "%x" 10 ## returns a, 10 in hexadecimal printf "%X" 10 ## returns A, 10 in hexadecimal (capital letter) printf "%f" 10 ## returns 10.000000 printf "%e" 10 ## returns 1.000000e+01 printf "%E" 10 ## returns 1.000000E+01 printf "%.5f" 10.25 ## returns 10.25000 printf "%.5g" 10.25 ## returns 10.25 printf "%.5G" 10.25 ## returns 10.25 printf "%a" 10.25 ## returns 0x1.48p+3 printf "%A" 10.25 ## returns 0X1.48P+3 ## escaping backslash printf "\\%s\\%s" "home" "user" ## returns \home\user printf "\a%s" "warning" ## returns warning with bell sound printf "%s\b%s" "hello" "world" ## returns hellworld printf "\f%s\n" "hello world" ## returns hello world clearing the screen printf "%s\n" "hello world" ## prints a new line after hello world printf "%s\r%s" "hello" "world" ## returns world printf "%s\t%s" "hello" "world" ## returns hello world printf "\v%s\n" "hello" ## returns vertical tab and then hello printf "%s\'s" "debjeet" ## returns debjeet's printf "'%s'" "debjeet" ## returns 'debjeet' printf "%s\"s" "debjeet" ## returns debjeet"s printf "\"%s\"" "debjeet" ## returns "debjeet" printf "%s\?" "how are you" ## returns how are you? printf "%s%%" "10" ## returns 10% printf "\e[32m%s" "hello" ## returns hello in green color printf "\004" ## returns ╝ printf "\x5E" ## returns ^ printf "%s" "A\t" "B\t" "C\t" ## returns A\tB\tC\t printf "%b" "A\t" "B\t" "C\t" ## returns A B C printf "%q" "a$ b*c#d'%" ## returns a\$\ b\*c#d\'% ## field modifier printf "%s" "debjeet" ## returns debjeet printf "%20s" "debjeet" ## returns debjeet printf "%*s" 20 "debjeet" ## returns debjeet printf "%.3s" "debjeet" ## returns deb printf "%-5s" "deb" "jeet" ## returns deb jeet printf "%05d" 3 ## returns 00003 printf "%d%05d" 10 5 ## returns 100005 printf "%f" 123.123 ## returns 123.123000 printf "%.5f" 123.123 ## returns 123.12300 printf "%08.2f" 12345.12845 ## returns 12345.13 |
Hope you have enjoyed this article. In the next blog post, we will discuss cat command in Linux.