Difference between revisions of "Printf bash builtin"

From Free Knowledge Base- The DUCK Project: information for everyone
Jump to: navigation, search
Line 1: Line 1:
 
The printf command is one of a set of [[Bash Builtin Commands]].  It provides a method to print pre-formatted text, expanding shells script standard output over the echo command. See format and example below:
 
The printf command is one of a set of [[Bash Builtin Commands]].  It provides a method to print pre-formatted text, expanding shells script standard output over the echo command. See format and example below:
  
printf <FORMAT> <ARGUMENTS...>
+
  printf <FORMAT> <ARGUMENTS...>
printf "Last name: %s\nName: %s\n" "$SURNAME" "$LASTNAME"
+
  printf "Last name: %s\nName: %s\n" "$SURNAME" "$LASTNAME"
  
 
printf does not automatically line feed unlike the echo command. LF is not implied, it must be stated. See two equivalents below:
 
printf does not automatically line feed unlike the echo command. LF is not implied, it must be stated. See two equivalents below:
  
echo "Hello World"
+
  echo "Hello World"
printf "Hello World\n"
+
  printf "Hello World\n"
printf "%s\n" "Hello World"  
+
  printf "%s\n" "Hello World"  
  
 
Use of %s formatting argument will also allow it to be applied to multiple lines.  The following example will output a linefeed for each of the lines in the printf statement.
 
Use of %s formatting argument will also allow it to be applied to multiple lines.  The following example will output a linefeed for each of the lines in the printf statement.
  
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"
  
 
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 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 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.
 +
 +
  printf "%s\t%s\n" "cannolis" "plums" "guacamole" "tangerine"
 +
 +
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:

Revision as of 12:52, 9 February 2014

The printf command is one of a set of Bash Builtin Commands. It provides a method to print pre-formatted text, expanding shells script standard output over the echo command. See format and example below:

 printf <FORMAT> <ARGUMENTS...>
 printf "Last name: %s\nName: %s\n" "$SURNAME" "$LASTNAME"

printf does not automatically line feed unlike the echo command. LF is not implied, it must be stated. See two equivalents below:

 echo "Hello World"
 printf "Hello World\n"
 printf "%s\n" "Hello World" 

Use of %s formatting argument will also allow it to be applied to multiple lines. The following example will output a linefeed for each of the lines in the printf statement.

 printf "%s\n" "I have eaten" "the plums" "that were in" "the icebox"

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 %% 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 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.

 printf "%s\t%s\n" "cannolis" "plums" "guacamole" "tangerine"

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: