commands:builtin:printf

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
commands:builtin:printf [2016/07/08 08:03]
izabera using $() in ksh is stupid
commands:builtin:printf [2016/11/30 15:39] (current)
medievalist [differences from awk printf]
Line 277: Line 277:
 Please read the manpage of ''​strftime(3)''​ to get more information about the supported formats. Please read the manpage of ''​strftime(3)''​ to get more information about the supported formats.
  
-==== Using printf inside of awk ====+===== differences from awk printf =====
  
-Here'​s ​the gotcha:+Awk also derives its //​printf()//​ function from C, and therefore has similar format specifiers. ​ However, in all versions of awk the space character is used as a string concatenation operator, so it cannot be used as an argument separator. ​ **Arguments to awk printf must be separated by commas.** ​ Some versions of awk do not require printf arguments to be surrounded by parentheses,​ but you should use them anyway to provide portability. 
 + 
 +In the following example, the two strings are concatenated by the intervening space so that no argument remains to fill the format.
  
 <​code>​ <​code>​
- 
-$ printf "​%s\n"​ "​Foo"​ 
-Foo 
  
 $ echo "​Foo"​ | awk '{ printf "​%s\n"​ $1 }' $ echo "​Foo"​ | awk '{ printf "​%s\n"​ $1 }'
Line 294: Line 293:
 </​code>​ </​code>​
  
-One fix is to use commas to separate ​the format from the arguments:+Simply replacing ​the space with a comma and adding parentheses yields correct awk syntax.
  
 <​code>​ <​code>​
-$ echo "​Foo"​ | awk '{ printf "​%s\n",​ $1 }'+$ echo "​Foo"​ | awk '{ printf"​%s\n",​ $1 }'
 Foo Foo
 </​code>​ </​code>​
  
-Or, use **printf** ​the way that awk wants you to:+With appropriate metacharacter escaping ​the bash printf can be called from inside ​awk (as from perl and other languages that support shell callout) as long as you don't care about program efficiency or readability.
  
 <​code>​ <​code>​
-echo "​Foo"​ | awk '{ printf $1 "\n" }'+echo "​Foo"​ | awk '​{ ​system( "printf ​\"​%s\\n \" \"" ​$1 "​\"​" ​ ) }'
 Foo Foo
 </​code>​ </​code>​
- 
-But then you lose the ability to pad numbers, set field widths, etc. that **printf** has. 
  
 ===== Differences from C, and portability considerations ===== ===== Differences from C, and portability considerations =====
Line 328: Line 325:
   * mksh has no built-in printf by default (usually). There is an unsupported compile-time option to include a very poor, basically unusable implementation. For the most part you must rely upon the system'​s ''/​usr/​bin/​printf''​ or equivalent. The mksh maintainer recommends using ''​print''​. The development version (post- R40f) adds a new parameter expansion in the form of ''​${name@Q}''​ which fills the role of ''​printf %q''​ -- expanding in a shell-escaped format.   * mksh has no built-in printf by default (usually). There is an unsupported compile-time option to include a very poor, basically unusable implementation. For the most part you must rely upon the system'​s ''/​usr/​bin/​printf''​ or equivalent. The mksh maintainer recommends using ''​print''​. The development version (post- R40f) adds a new parameter expansion in the form of ''​${name@Q}''​ which fills the role of ''​printf %q''​ -- expanding in a shell-escaped format.
  
-  * ksh93 optimizes builtins run from within a command substitution and which have no redirections to run in the shell'​s process. Therefore the ''​printf -v''​ functionality can be closely matched by ''​var=$printf ...;}''​ without a big performance hit.+  * ksh93 optimizes builtins run from within a command substitution and which have no redirections to run in the shell'​s process. Therefore the ''​printf -v''​ functionality can be closely matched by ''​var=$(printf ...)''​ without a big performance hit.
 <​code>​ <​code>​
 # Illustrates Bash-like behavior. Redefining printf is usually unnecessary / not recommended. # Illustrates Bash-like behavior. Redefining printf is usually unnecessary / not recommended.
Line 337: Line 334:
             nameref x=$1             nameref x=$1
             shift             shift
-            x=$command printf "​$@"​;}+            x=$(command printf "​$@"​)
             ;;             ;;
         *)         *)
  • commands/builtin/printf.1467965038.txt
  • Last modified: 2016/07/08 08:03
  • by izabera