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}" | sortIn 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
tee Command Reference for CI Scriptstee writes stdin to a file and stdout at once in CI logging. Reference for -a append, multiple files, and the…
sed Command Reference for CI Scriptssed is a stream editor for find-and-replace in CI builds. Reference for -i, -e, -E, and s///, plus the GNU-vs…
envsubst Command Reference for CI Scriptsenvsubst substitutes environment variables into templates in CI. Reference for variable lists and the over-su…
date Command Reference for CI Scriptsdate formats and computes timestamps in CI for tags and filenames. Reference for +FORMAT, -u UTC, and epoch s…