'printf -v'有什么作用?

sja*_*jas 7 bash printf

printf -v网上的bash脚本示例中遇到过几次这个问题之后,以及有关stackoverflow的几个问题,我在printf联机帮助页中找不到合适的解释.

man printf或者man 3 printf不帮助我.

我在哪里寻找?

sja*_*jas 10

printflinux中有几个命令:

  1. printf作为已知的C函数.(描述man 3 printf)
  2. GNU printf,位于/usr/bin/printf.(见man printf)
  3. bash的printf built-in.(参见man bash本节的条目SHELL BUILTIN COMMANDS).还可以找到帮助help printf,它将显示联机帮助页中的内置说明.

要了解您确切需要什么,请使用type <command>以找出特别使用的内容:

root@pi:~# type printf
printf is a shell builtin
Run Code Online (Sandbox Code Playgroud)

所以3号是这里的解决方案:

printf [-v var] format [arguments]

The -v option causes the output to be assigned to the variable var rather than being printed to the standard output.

摘录自此处:

printf [-v var] format [arguments]
    Write  the formatted arguments to the standard output under the control 
    of the format.  The -v option causes the output to be assigned to the 
    variable var rather than being printed to the standard output.

   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(1) 
    format specifications, printf interprets the following extensions:

       %b     causes  printf  to  expand backslash escape sequences in the 
              corresponding argument (except that \c terminates output, 
              backslashes in \', \", and \? are not removed, and octal escapes 
              beginning with \0 may contain up to four digits).

       %q     causes printf to output the corresponding argument in a format that 
              can be reused as shell input. 

       %(datefmt)T
              causes  printf to output the date-time string resulting from using 
              datefmt as a format string for strftime(3). The corresponding 
              argument is an integer representing the number of seconds  since  
              the epoch. 

              Two  special argument values may be used: 

                  -1 represents the current time, and 
                  -2 represents the time the shell was invoked.

          Arguments  to  non-string  format  specifiers are treated as 
          C constants, except that a leading plus or minus sign is allowed, 
          and if the leading character is a single or double quote, 
          the value is the ASCII value of the following character.

          The  format  is  reused as necessary to consume all of the arguments.  
          If the format requires more arguments than are supplied, the extra 
          format specifications behave as if a zero value or null string, 
          as appropriate, had been supplied.  

          The return value is zero on success, non-zero on failure.
Run Code Online (Sandbox Code Playgroud)

  • 可以通过运行`help printf`找到shell内置`printf`的帮助. (5认同)
  • @chepner 谢谢,将您的答案添加到答案中。 (2认同)