Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Linux & Mac & Bash Command Dictionary

  1. Home
  2. Linux & Mac & Bash Command Dictionary
  3. echo / printf

echo / printf

Since: echo 全Linux
macOS(2001 Cheetah)
Bash 1.0(1989)
printf 全Linux
macOS(2001 Cheetah)
Bash 2.0(1996)

echo is a command that prints strings to standard output. printf works similarly to the C function of the same name, supporting format specifiers for controlling newlines and formatting numbers.

Syntax

echo [options] [string...]
printf format [arguments...]

Options and Format Specifiers

Command / FormatDescription
echo stringPrints the string followed by a newline.
echo -n stringPrints the string without a trailing newline.
echo -e stringInterprets backslash escape sequences (e.g., \n, \t, \033[).
echo $variableExpands and prints the value of a variable.
printf "%s\n" stringOutputs a string using a format specifier.
printf "%d\n" numberOutputs a value as an integer.
printf "%05d\n" numberOutputs a value zero-padded to 5 digits.
printf "%.2f\n" numberOutputs a value with 2 decimal places.
printf "%10s\n" stringOutputs a string right-aligned in a 10-character field.
printf "%-10s\n" stringOutputs a string left-aligned in a 10-character field.

Sample Code

Print a string using echo.

echo "Hello, World!"
Hello, World!

Expand and print the value of a variable.

sample_greeting.sh
NAME="Okabe Rintaro"
echo "Hello, $NAME!"
bash greeting.sh
Hello, Okabe Rintaro!

Use the -n option to suppress the trailing newline. This is useful for inline prompts.

sample_echo_n.sh
echo -n "Processing..."
echo " done!"
bash echo_n.sh
Processing... done!

Use the -e option to interpret escape sequences. This allows colored output.

sample_color.sh
echo -e "\033[32mSuccess!\033[0m"
echo -e "\033[31mError!\033[0m"
bash color.sh
Success!
Error!

Use printf for formatted output, including integers and zero-padding.

printf "%d\n" 42
42

Output a zero-padded sequence. Useful for generating filenames.

sample_numbering.sh
for i in 1 2 3; do
    printf "file_%03d.txt\n" $i
done
bash numbering.sh
file_001.txt
file_002.txt
file_003.txt

You can also enter a for loop directly in the terminal. After pressing Enter following do, a > prompt appears — this means input is still expected. Enter done to execute.

for i in 1 2 3; do
    printf "file_%03d.txt\n" $i
done
file_001.txt
file_002.txt
file_003.txt

Use printf to format tabular output. %-10s is left-aligned in a 10-character field, and %5d is right-aligned in a 5-character field.

sample_table.sh
printf "%-15s %5s\n" "Name" "Score"
printf "%-15s %5d\n" "Okabe Rintaro" 95
printf "%-15s %5d\n" "Makise Kurisu" 87
bash table.sh
Name            Score
Okabe Rintaro      95
Makise Kurisu      87

Common Mistakes

Common Mistake 1: echo -e behavior varies by shell

The -e option for escape sequences is not supported by all shells. On some systems it prints -e literally.

/bin/sh -c 'echo -e "line1\nline2"'
-e line1\nline2

Use printf for portable escape sequence handling.

printf "line1\nline2\n"
line1
line2

Common Mistake 2: printf without a newline leaves the cursor on the same line

Unlike echo, printf does not add a newline automatically. Forgetting \n causes the next prompt to appear on the same line.

printf "Hello"
Hello$
(next prompt appears immediately after "Hello")

Add \n at the end of the format string.

printf "Hello\n"
Hello

Notes

For portable scripts, '$ printf' is preferred over '$ echo -e'. The behavior of '$ echo -e' varies between shells and implementations, whereas '$ printf' is defined by the POSIX standard and behaves consistently.

To read a line from standard input, use read. To write output to a file, see redirection (>).

If you find any errors or copyright issues, please .