Skip to content
Latchkey

printf Command Reference for CI Scripts

printf formats and prints arguments using a C-style format string.

printf is the portable, predictable alternative to echo. It does not append a newline unless you ask, and it handles escapes and formatting the same way everywhere.

Common flags/usage

  • %s: insert a string argument
  • %d / %x: insert a decimal / hex integer
  • %b: interpret backslash escapes in the argument
  • \n / \t: newline / tab in the format string
  • Format reuses for extra arguments, looping over them

Example

shell
printf '%s=%s\n' "VERSION" "${VERSION}" >> "${GITHUB_ENV}"
printf '::error::%s\n' "Build failed for ${GITHUB_SHA}"
printf '%s\n' "${FILES}" | sort

In CI

Prefer printf over echo when output must be exact: echo -e and echo -n behave differently across shells and the dash /bin/sh used in many containers. printf '%s' "$VAR" prints a variable literally even if it starts with a dash or contains escape sequences, which echo may misinterpret.

Key takeaways

  • printf is portable where echo -e and echo -n differ across shells.
  • Always include an explicit \n; printf does not add one for you.
  • Use %s to print untrusted values literally without escape interpretation.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →