2015-05-07 21 views
6

Dopo aver riscontrato più volte questo printf -v in esempi di script bash in rete, oltre a diverse domande su StackOverflow, non sono riuscito a trovare una spiegazione adeguata nelle manpage printf.Che cosa fa "printf -v"?

man printf o man 3 printf non mi aiutano.

Dove devo cercare?

+0

Perché le risposte di auto ottengono sempre punteggi negativi? (...) – sjas

risposta

9

Esistono diversi printf comandi all'interno Linux:

  1. printf come funzione C nota. (descritto in man 3 printf)
  2. GNU printf, che si trova in /usr/bin/printf. (vedi man printf)
  3. bash's printf built-in. (vedere man bash e vedere la relativa voce nella sezione SHELL BUILTIN COMMANDS). Anche l'aiuto può essere trovato tramite help printf, che mostrerà la descrizione integrata dalla manpage.

Per scoprire, quello che esattamente bisogno, utilizzare type <command> per scoprire quello che viene utilizzato in particolare:

[email protected]:~# type printf 
printf is a shell builtin 

Quindi numero 3 è la soluzione qui:

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.

Estratto preso da qui:

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. 
+2

L'aiuto per la shell built-in 'printf' può essere trovato eseguendo' help printf'. – chepner

+0

@chepner Grazie, aggiungerai la risposta alla risposta. – sjas