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.

  1. Home
  2. Bash Dictionary
  3. echo / printf

echo / printf

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.

greeting.sh
NAME="Alice"
echo "Hello, $NAME!"
bash greeting.sh
Hello, Alice!

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

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.

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.

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.

table.sh
printf "%-10s %5s\n" "Name" "Score"
printf "%-10s %5d\n" "Alice" 95
printf "%-10s %5d\n" "Bob" 87
bash table.sh
Name       Score
Alice         95
Bob           87

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 .