In Linux, echo and printf are 2 commands that are used commonly when we need to print text to the screen. In this article, we will learn how to use and the difference between these 2 commands.
I. echo
1. Print text by echo command
To print a text string, we can use the echo command as follows:
$ echo Welcome to Bash
Welcome to Bash
or
$ echo 'Welcome to Bash'
Welcome to Bash
or
$ echo "Welcome to Bash" & nbsp;
Welcome to Bash
All 3 methods above return same result, but each method has own using purpose and circumstance.
Example:
$ echo Hello World!
Hello the world!
$ echo 'Hello World!'
Hello the world!
$ echo "Hello World!"
- bash event not found: error! "
The difference between these 3 methods is as below:
- When using echo without squotes (”), we cannot use semicolon (;), because in this circumstance the semicolon acts as the separator between commands in the Bash shell.
Example:
$ echo hello; Hello
Hello
-bash: hello: command not found
- Variable substitution has no effect within quotes ”.
Example:
VAR="hello"
echo '$VAR' => $ VAR
echo "$VAR" => hello
2. Do not add new line in output text of echo
By default, after outputting the results, echo will automatically add a new line.
$ echo "Newline"
New line
$
If we don’t want echo to automatically add new line, add the -n flag after echo like this:
$ echo -n "Newline"
Newline $
3. Format result with colors
The following list is color codes that are often used with string characters.
- reset – 0
- black – 30
- red – 31
- blue – 32
- yellow – 33
- blue – 34
- magenta – 35
- cyan – 36
- white – 37
To print a colored text, use echo command as below:
$ echo -e "\e[31m This is red text\e[0m"
This is red text
With:
- \e [31m: Escape string to set color into red (color code is 31).
- \e [0m: reset to default color.
Replace 31 with the color code you want.
With background colors, below is commonly used color codes.
- reset – 0
- black – 40
- red- 41
- green – 42
- yellow – 43
- blue – 44
- magenta – 45
- cyan – 46
- white – 47
Example:
$ echo -e "\e[42m Green Background \e[0m"
II. printf
1. Print text with printf
Another command used to print text is printf command. It uses arguments like the printf command in the C programming language.
Unlike echo, the text to be printed must be completely enclosed in single or double quotes and printf does not automatically add new line like echo command.
$ printf "Hello World"
Hello World $
$ printf 'Hello World'
Hello World $
$ printf Hello World
Hello $
2. Format the output string with printf
Example:
#!/bin/bash
#Filename: printf.sh
printf "%-5s %-10s %-4s\n" No Name Mark
printf "%-5s %-10s %-4.2f\n" 1 Sarath 80.3456
printf "%-5s %-10s %-4.2f\n" 2 James 90.9989
printf "%-5s %-10s %-4.2f\n" 3 Jeff 77.564
Run above script
$ bash printf.sh
Output result
No Name Mark
1 Sarath 80.35
2 James 91.00
3 Jeff 77.56
With:
- %s, %c, %d and %f are format substitution characters that an argument can be placed in.
- %-5s
- – : left-aligned string of characters. Without – the string will be right-aligned
- 5: string-specific length. In case the string is less than 5 characters long, the space characters will be inserted to make it long enough.
- %-4.2f
- .2 : round to 2 decimals
- /n: newline