Linux Command Reference ยท Shell Builtins
Bash printf -v option
Reference for printf -v in Bash: store formatted text in a variable or array element without a subshell, build padded strings, dates and quoted values.
printf -v NAME FORMAT [ARGUMENTS] is the Bash builtin printf with one addition: instead of writing the result to standard output, it stores it in the variable NAME. That replaces the common pattern NAME=$(printf ...), and the difference matters: command substitution forks a subshell, which costs time in a loop, and it strips trailing newlines. printf -v does neither.
NAME can be a plain variable, an array element (arr[3]), or a variable name computed at run time, which makes it the safe, eval-free way to assign to an indirect variable.
printf -v padded '%05d' 42 # padded=00042 printf -v 'list[2]' '%s' value # array element
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
printf -v VAR FORMAT [ARGUMENTS ...]
| Name | Description |
|---|---|
VAR |
Variable (or array[index]) to set. Must be a valid name. |
FORMAT |
A format string with %s %d %i %f %x %o %c %q %b %% and flags, width and precision (%*s takes the width from an argument). |
ARGUMENTS |
Values for the conversions. If there are more arguments than conversions the format is reused; missing ones are treated as empty or zero. |
Returns: 0 on success; non-zero for an invalid variable name, a read-only variable or a bad format.
Extras: %q quotes the argument for reuse as shell input; %b interprets backslash escapes in the argument; %(datefmt)T formats a time (seconds since the epoch, -1 for now, -2 for shell start).
Usage notes
- No subshell.
printf -vruns in the current shell, so it is far faster than$(printf ...)inside a loop of thousands of iterations. - Newlines are preserved. Unlike command substitution, trailing newlines stay in the variable.
- Overwrites, never appends. To append, format into a temporary and concatenate:
printf -v tmp ...; out+=$tmp. - Quote the name. Write
printf -v 'arr[0]'with quotes so the shell does not treat[0]as a glob pattern. - Locale and decimals.
%fuses the locale's decimal separator and the argument must be a valid number in that notation. Integers and floats are not interchangeable:%dof3.5is an error. - Octal surprise. A leading zero means octal:
printf '%d' 010is8. Use10#010to force decimal. - Not POSIX.
-vexists in Bash and a few others; the regularprintfworks everywhere. - Instead of
eval. Assign to a computed name withprintf -v "$name" '%s' "$value"; seeeval.
Related
printf,echo,declare,read,date, Command substitution- Lessons: printf and date, Variables, Arrays