Use Cases and Examples

Bash [.inline-code]printf[.inline-code] prints text on a terminal with multiple formatting options. It’s probably most useful to print in a shell script, but can be used on the command line as well.

 $ name = Sam
 $ printf "Hi $name"
 Hi Sam

In cases where we want more control over how content is displayed, we can leverage the additional formatting specifiers and arguments to fit our needs.

[#printf-syntax]printf syntax[#printf-syntax]

[.inline-code]printf [-v var] [format specifiers] [arguments][.inline-code]

var: optional and if used, the output will not be printed but will be assigned to the variable. This is not to be confused with printing variables which you can do by prefixing the variable with [.inline-code]$[.inline-code].

 # This first command stores the content as a variable and will not output anything
 $ printf -v fooBar "Welcome"
 $ printf "$fooBar"
  
 "Welcome"

format specifiers: string(s) that may contain one or more of:

  • Normal characters
  • Backslash-escaped characters
  • [.inline-code]\b[.inline-code] - backspace character
  • [.inline-code]\n[.inline-code] - newline character
  • [.inline-code]\t[.inline-code] - horizontal tab space
  • [.inline-code]\v[.inline-code] - vertical tab space
  • [.inline-code]\”[.inline-code] - quotation character
  • [.inline-code]\\[.inline-code] - backslash character
 $ printf "Hellow\\b \\"John\\\\Joe\\"\\n"
  Hello "John\\Joe"

Conversion specifications (e.g. to format string or format number)

  • [.inline-code]%s[.inline-code] - to treat as a string
  • [.inline-code]%c[.inline-code] - to treat as a single character
  • [.inline-code]%f[.inline-code] - to treat as a floating number
  • [.inline-code]%d[.inline-code] - to treat as a signed integer
  • [.inline-code]%u[.inline-code] - to treat as an unsigned integer
  • [.inline-code]%%[.inline-code] - to print the percentage symbol
  • [.inline-code]%X[.inline-code] - to print with X character wide widths specified
  • [.inline-code]%.X[.inline-code] - to print with a precision modifier of X characters
  • [.inline-code]%- -[.inline-code] to print left-justified. note: by default it is right-justified.
 $ printf "%s are %d%% off! OM%c!" "Shirts" 5 "GGGGG"
 "Shirts are 5% off! OMG!"

 $ printf "%5s: %f\\n" "Abe" 3.5 "John" 5 "It" 8.221
 Abe: 3.500000
 
 John: 5.000000
  It: 8.221000
  
 $ printf "%-5s: %f\\n" "Abe" 3.5 "John" 5 "It" 8.221
 Abe  : 3.500000
 John : 5.000000
 It   : 8.221000

arguments: can be any number of values and/or variables. In the case that there are more arguments than format specifiers, then the arguments will get reused.

 # number of arguments exceeds format specifiers
  $ printf "One %d Two %d Three %d\\n" 1 2 3 4
  One 1 Two 2 Three 3
  One 4 Two 0 Three 0
  
  # number of arguments is fewer than format specifiers
  $ printf "One %d Two %d Three %d\\n" 1 2
  One 1 Two 2 Three 0

Tip: If you are on zsh and you may be seeing a % at the end of each line. This is zsh's way of noting that the preceding command output was a partial line. You can disable it by updating the PROMPT_EOL_MARK configuration within your ~/.zshrc file.

[#readable-content][.inline-code]printf[.inline-code] makes it easy for engineers to output readable content[#readable-content] 

Being able to use [.inline-code]printf[.inline-code] for all of its functionalities helps us as engineers understand our code easier and in a human-friendly way. For example, imagine you were trying to debug a script with the following variables.

 USD=1.0
  CNY=7.188832
  BZD=55.442
  TWD=31.7

In the example below, we don’t leverage the variable printing that [.inline-code]printf[.inline-code] has, so we end up hardcoding the currency amounts. Additionally, it is harder to read because it is all on one line, and all of the variables have different precisions.

 $ printf "Current Currency Rates USD: 1 / CNY: 7.188832 / BZD: 0.55 /  TWD: 31.7"
  Current Currency Rates USD: 1 / CNY: 7.188832 / BZD: 0.55 /  TWD: 31.7

Instead, if we utilize the capabilities of [.inline-code]printf[.inline-code] we can write legible statements that can be modified and built on top of. Below, we are printing the variables and formatting the output with the width specified and float precision.

 $ printf "Current Currency Rates\\n"
 $ printf "%4s: %.2f\\n" "USD" $USD "CNY" $CNY "BZD" $BZD "TWD" $TWD
 Current Currency Rates
  USD: 1.00
  CNY: 7.19
  BZD: 0.55
  TWD: 31.70

[#echo-vs-printf][.inline-code]echo[.inline-code] vs [.inline-code]printf[.inline-code][#echo-vs-printf]

When writing scripts, [.inline-code]echo[.inline-code] is the simplest command to print to the standard output. By default, it adds a new line so that you don’t need to! You can print strings and variables and add options to escape backslashes.

 $ echo "Hello World"
 "Hello World"
 
 $ foo=100
 $ echo "I have $foo dollars"
 "I have 100 dollars"
 
 $ echo -e "Hellow\\b \\nWorld"
 Hello
 World

While [.inline-code]echo[.inline-code] is straightforward to use, there are noted inconsistencies with its implementation, and is considered “non-portable”. Here is a stackexchange post that elaborates further. Instead, the [.inline-code]printf[.inline-code] command has become the replacement command and offers a wider range of capabilities in the way it formats and prints the arguments.

[#printf-vs-sprintf-vs-fprintf][.inline-code]printf[.inline-code] vs [.inline-code]sprintf[.inline-code] vs [.inline-code]fprintf[.inline-code][#printf-vs-sprintf-vs-fprintf]

In addition to the [.inline-code]printf[.inline-code] function, there are two other similar functions you may have seen.

[.inline-code]printf[.inline-code] is arguably the most common and used to print to the standard output stdout

[.inline-code]sprintf[.inline-code] does not print it to the standard output but rather stores it on a buffer

[.inline-code]fprintf[.inline-code] does not print to the standard output but rather prints to a file

Experience the power of Warp

  • Write with an IDE-style editor
  • Easily navigate through output
  • Save commands to reuse later
  • Ask Warp AI to explain or debug
  • Customize keybindings and launch configs
  • Pick from preloaded themes or design your own