printf
The command that prints text according to a format string, such as printf '%s\n' "$name".
In more detail
%s stands for a string, %d for a whole number and %-12s for a string padded to 12 characters, and escape sequences such as \n work in the format. If there are more arguments than placeholders, the format repeats, which makes printf '[%s]\n' "$@" show each argument on its own line. It behaves the same in every shell, unlike echo.
Examples
printf '%-12s %5s\n' web01 443printf 'third line\n' >> notes.txtThe format string says how to lay out the remaining arguments: %s inserts one argument as text and \n starts a new line. When there are more arguments than placeholders, printf repeats the format, which is why printf '%s\n' a b c prints three lines. It behaves the same on Linux and macOS, unlike echo, which makes it the portable choice for scripts.printf '%s\n' NAME=Example VERSION=1 prints two lines, one per argument.Related terms
- echo — The shell command that prints its arguments as a line of text.
- escape sequence — A backslash followed by a character that together stand for something hard to type directly, such as \n for a line break.
- shell — The program that reads the commands you type, runs them and shows the results; sh and bash are common ones.
- standard output and standard error — The two channels a program writes normal output (stdout) and error messages (stderr) to, plus stdin for input.
Taught in
- Making, Copying, Moving And Removing — Bash Scripting & Command Line Automation
- What Bash Does To A Line Before It Runs It — Bash Scripting & Command Line Automation
- Single Quotes, Double Quotes, Backslash — Bash Scripting & Command Line Automation
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- Arrays, And Why Dollar At Is Not Dollar Star — Bash Scripting & Command Line Automation
- Standard Input, Output And Error — Bash Scripting & Command Line Automation
- Redirection: Truncate, Append, Both Streams — Bash Scripting & Command Line Automation
- Pipelines, tee And Process Substitution — Bash Scripting & Command Line Automation
- Finding Lines: grep And Regular Expressions — Bash Scripting & Command Line Automation
- Shell Variables, The Environment And export — Bash Scripting & Command Line Automation
- Command Substitution And Subshells — Bash Scripting & Command Line Automation
- Loops: for, while, until, break, continue — Bash Scripting & Command Line Automation
- Script Input And Output: read, Heredocs, printf — Bash Scripting & Command Line Automation
- Functions, local And declare — Bash Scripting & Command Line Automation
- Arguments: Dollar One, Dollar At, shift — Bash Scripting & Command Line Automation
- trap, Cleanup And Temporary Files — Bash Scripting & Command Line Automation
- ShellCheck — Bash Scripting & Command Line Automation
- Style That Prevents Bugs — Bash Scripting & Command Line Automation
- POSIX sh Versus Bash — Bash Scripting & Command Line Automation
- Permissions And Ownership — Bash Scripting & Command Line Automation
- find And xargs — Bash Scripting & Command Line Automation
- Scheduling, Monitoring And A Script That Reports — Bash Scripting & Command Line Automation
- Envelope Encryption & Data Protection with KMS — Cloud Security & DevSecOps Engineering
- Why Version Control, And Why Git Won — Advanced Git Internals & Distributed Workflows
- The Three Areas: Working Tree, Index, Repository — Advanced Git Internals & Distributed Workflows
- Committing, And What A Good Message Says — Advanced Git Internals & Distributed Workflows
- Ignoring Files, And The Tracked File Trap — Advanced Git Internals & Distributed Workflows
- A Branch Is A Moving Pointer — Advanced Git Internals & Distributed Workflows
- HEAD, And Why Detached HEAD Is Not An Error — Advanced Git Internals & Distributed Workflows
- What A Remote Actually Is — Advanced Git Internals & Distributed Workflows
- What A Kernel Does, And What It Refuses To Do — Linux Fundamentals & Systems Administration
- What A Distribution Actually Is — Linux Fundamentals & Systems Administration
- The Filesystem Hierarchy — Linux Fundamentals & Systems Administration
- Inodes, Hard Links And What rm Removes — Linux Fundamentals & Systems Administration
- Everything Is A File: proc, sys And dev — Linux Fundamentals & Systems Administration
- Users, UIDs And The Password File — Linux Fundamentals & Systems Administration
- What A Process Is — Linux Fundamentals & Systems Administration
- The Layer Model, As Far As It Earns Its Place — Linux Fundamentals & Systems Administration
- TCP Versus UDP — Linux Fundamentals & Systems Administration
- The Diagnostic Ladder: Is It Up, Is It Named, Is It Routed — Linux Fundamentals & Systems Administration
- The Diagnostic Ladder: Is It Listening, Answering, Arriving — Linux Fundamentals & Systems Administration