Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'printf -v' do?

Tags:

bash

printf

After having come across several times this printf -v in bash script examples on the net, as well as several questions on stackoverflow, I could not find a proper explanation in the printf manpages.

man printf or man 3 printf do not help me.

Where do I have to look for?

like image 314
sjas Avatar asked May 07 '15 10:05

sjas


People also ask

What does printf do in C?

2.3. 1 C standard output (printf(), puts() and putchar()) The printf() function sends a formatted string to the standard output (the display). This string can display formatted variables and special control characters, such as new lines ('\n'), backspaces ('\b') and tabspaces ('\t'); these are listed in Table 2.1.

Why we are using printf?

C++ printf is a formatting function that is used to print a string to stdout. The basic idea to call printf in C++ is to provide a string of characters that need to be printed as it is in the program. The printf in C++ also contains a format specifier that is replaced by the actual value during execution.

What is the purpose of printf () function explain with example?

Printf() function is used to print the “character”, string, float, integer, octal, and hexadecimal values onto the output screen. We use printf() function with a %d format specifier to display the value of an integer variable.

Why do we use printf () and scanf () functions?

These functions are defined and declared in stdio. h header file. The 'f' in printf and scanf stands for 'formatted'. So, both the functions printf() and scanf() use codes within a format string to specify how output or input values should be formatted.


Video Answer


1 Answers

There exist several printf commands within linux:

  1. printf as the known C function. (described in man 3 printf)
  2. GNU printf, located in /usr/bin/printf. (see man printf)
  3. bash's printf built-in. (see man bash and see the entry for it at section SHELL BUILTIN COMMANDS). Also help can be found via help printf, which will show the built-in description from the manpage.

To find out, what you exactly need, use type <command> to find out what is used in particular:

root@pi:~# type -a printf
printf is a shell builtin
printf ist /usr/bin/printf

So Number 3 is the solution here:

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.

Excerpt taken from here:

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.
like image 177
sjas Avatar answered Sep 19 '22 19:09

sjas