【问题标题】:What does 'printf -v' do?“printf -v”有什么作用?
【发布时间】:2021-01-24 05:31:43
【问题描述】:

在网上的 bash 脚本示例中多次遇到此 printf -v 以及有关 stackoverflow 的几个问题后,我无法在 printf 手册页中找到正确的解释。

man printfman 3 printf 不要帮我。

我必须在哪里寻找?

【问题讨论】:

    标签: bash printf


    【解决方案1】:

    在 linux 中有几个printf 命令:

    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 -a printf
    printf is a shell builtin
    printf ist /usr/bin/printf
    

    所以第 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.
    

    【讨论】:

    • 运行help printf可以找到shell内置printf的帮助。
    • @chepner 谢谢您,将您的答案添加到答案中。
    猜你喜欢
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2023-03-28
    • 2011-01-23
    • 2015-01-08
    • 2023-03-16
    相关资源
    最近更新 更多