📝 Edit page
➕ Add page
printf
Print format
Named after printf
in C.
- Manpage on SS64.
Text
Print text - without trailing newline by default.
$ printf Hello
Hello
With newline.
$ printf "Hello\n"
Hello
Expression
$ X=abc
$ printf "$X\n"
abc
Format type
Integer
$ printf "%d\n" 5
5
$ DISTANCE=15
$ printf "Distance is %5d km\n" $DISTANCE
Distance is 15 km
Float
Use 6 decimal places by default.
$ printf "%f\n" 5
5.000000
Multiline
$ printf %s "\
with quotes we can echo
several lines at a time
"
Store
Store the output in a variable, without printing.
-v
Cause the output to be assigned to the variable var rather than being printed to the standard output.
$ printf -v MY_VAR "Hello $USER"
$ echo $MY_VAR
Hello mcurrin