Changes

Printf bash builtin

1,709 bytes added, 19:49, 9 February 2014
The following lines were added (+) and removed (-):
printf <FORMAT> <ARGUMENTS...>  printf <FORMAT> <ARGUMENTS...> printf "Last name: %s\nName: %s\n" "$SURNAME" "$LASTNAME"  printf "Last name: %s\nName: %s\n" "$SURNAME" "$LASTNAME" echo "Hello World"  echo "Hello World" printf "Hello World\n"  printf "Hello World\n" printf "%s\n" "Hello World"    printf "%s\n" "Hello World"   printf "%s\n" "I have eaten" "the plums" "that were in" "the icebox"  printf "%s\n" "I have eaten" "the plums" "that were in" "the icebox"==== Format strings ====The format is a character string which contains three types of objects: plain characters, which are simply copied to standard output, character escape sequences, which are converted and copied to the standard output, and format specifications, each of which causes printing of the next successive argument. In addition to the standard printf, `%b' causes printf to expand backslash escape sequences in the corresponding argument, and `%q' causes printf to output the corresponding argument in a format that can be reused as shell input.  To print a literal ''%'' (percent-sign), use ''<nowiki>%%</nowiki>'' in the format string.  Every format specifier expects an associated argument.  If the format requires more arguments than are supplied, the extra format specifications behave as if a zero value or null string.The format string interpretion is derived from the C ''printf()'' function family. Only format specifiers that end in one of the letters ''diouxXfeEgGaAcs'' are recognized.The following outputs 2 colums of text seperated by tabs.  There is an initial tab, then text, tab, text and linefeed for each line. Column alignment is only achieved in this case due to the character length of string 1 and 3.  This is not a way to commit columns.To print a literal ''%'' (percent-sign), use ''<nowiki>%%</nowiki>'' in the format string.  printf "%s\t%s\n" "cannoli" "bizcocho" "croissants" "eclair"__**Again:**__ Every format specifier expects an associated argument provided!The next example will create rigid columns of text without regards to the string length so long as the string doesn't exceed the column cell allocation.  It will also assign two formatting variables to contain all the arguments for the sake of readability on the printf statement lines.  The vi editor will be used to place commands within a bash shell script.  SHELL SCRIPT EXAMPLE:These specifiers have different names, depending who you ask. But they all mean the same: A placeholder for data with a specified format:  <nowiki>#/bin/bash</nowiki>   * format placeholder   <nowiki>#</nowiki>   * conversion specification  <nowiki>strHeadline===============================</nowiki>   * formatting token  <nowiki>strHeadline=$strHeadline$strHeadline</nowiki>   * ...  <nowiki></nowiki>  <nowiki>header="\n %-10s %8s %13s %7s\n"</nowiki>  <nowiki>format=" %-10s %08d %10s %11.2f\n"</nowiki>   <nowiki></nowiki>   <nowiki>width=43</nowiki>   <nowiki>printf "$header" "ITEM NAME" "STOCK QTY" "DEPARTMENT" "PRICE"</nowiki>  <nowiki>printf "%$width.${width}s\n" "$strHeadline"</nowiki>  <nowiki></nowiki>  <nowiki>printf "$format" \</nowiki>  <nowiki>cannolis 140005 bakery 20 \</nowiki>  <nowiki>doughnuts 140049 bakery 65.656 \</nowiki>  <nowiki>danish 140314 bakery .7 \</nowiki>  <nowiki>sardines 110006 canned 1.59001</nowiki>^Format^Description^Breakdown of formatting arguments:|''%b''|Print the associated argument while interpreting backslash escapes in there|* %-10s  (line 6): '''string''' the - means format form left align over 10 spaces|''%q''|Print the associated argument **shell-quoted**, reusable as input|* %8d  (line 7): '''signed decimal number''' with 8 places |''%d''|Print the associated argument as **signed decimal** number|* %11.2f  (line 7): '''floating point number''' to 9 places with 2 decimals|''%i''|Same as ''%d''||''%o''|Print the associated argument as **unsigned octal** number||''%u''|Print the associated argument as **unsigned decimal** number||''%x''|Print the associated argument as **unsigned hexadecimal** number with lower-case hex-digits (a-f)||''%X''|Same as ''%x'', but with upper-case hex-digits (A-F)||''%f''|Interpret and print the associated argument as **floating point** number||''%e''|Interpret the associated argument as **double**, and print it in ''<N>±e<N>'' format||''%E''|Same as ''%e'', but with an upper-case ''E'' in the printed format||''%g''|Interprets the associated argument as **double**, but prints it like ''%f'' or ''%e''||''%G''|Same as ''%g'', but print it like ''%E''||''%c''|Interprets the associated argument as **char**: only the first character of a given argument is printed||''%s''|Interprets the associated argument literally as string||''%n''|Assigns the number of characters printed so far to the variable named in the corresponding argument. Can't specify an array index. If the given name is already an array, the value is assigned to the zeroth element.||''%a''|Interprets the associated argument as **double**, and prints it in the form of a C99 [[http://www.exploringbinary.com/hexadecimal-floating-point-constants/ | hexadecimal floating-point literal]].||''%A''|Same as ''%a'', but print it like ''%E''||''%(FORMAT)T''|output the date-time string resulting from using ''FORMAT'' as a format string for ''strftime(3)''. The associated argument is the number of seconds since Epoch, or ''-1'' (current time) or ''-2'' (shell startup time). If no corresponding argument is supplies, the current time is used as default||''%%''|No conversion is done. Produces a ''%'' (percent sign)|Some of the mentioned format specifiers can modify their behaviour by getting a format modifier:=== escape codes ===   === format arguments === Additional arguments available to printf for formatting* %b Print the associated argument while interpreting backslash escapes in there* %q Print the associated argument shell-quoted, reusable as input* %d Print the associated argument as signed decimal number* %i Same as %d* %o Print the associated argument as unsigned octal number* %u Print the associated argument as unsigned decimal number* %x Print the associated argument as unsigned hexadecimal number with lower-case hex-digits (a-f)* %X Same as %x, but with upper-case hex-digits (A-F)* %f Interpret and print the associated argument as floating point number* %e Interpret the associated argument as double, and print it in <N>±e<N> format* %E Same as %e, but with an upper-case E in the printed format* %g Interprets the associated argument as double, but prints it like %f or %e* %G Same as %g, but print it like %E* %c Interprets the associated argument as char: only the first character of a given argument is printed* %s Interprets the associated argument literally as string* %n Assigns the number of characters printed so far to the variable named in the corresponding argument. Can't specify an array index. If the given name is already an array, the value is assigned to the zeroth element.* %a Interprets the associated argument as double, and prints it in the form of a C99 hexadecimal floating-point literal.* %A Same as %a, but print it like %E* %(FORMAT)T output the date-time string resulting from using FORMAT as a format string for strftime(3). The associated argument is the number of seconds since Epoch, or -1 (current time) or -2 (shell startup time). If no corresponding argument is supplies, the current time is used as default === modifiers === {{:sparse entry}} [[Category:Computer_Technology]][[Category:Linux]]
Bureaucrat, administrator
16,192
edits