Linux echo Command
The echo command in Linux is used to display a line of text or string passed as an argument. It is commonly used in shell scripts and batch files to output text or variables.
Syntax
echo [OPTION]... [STRING]...
Options
-n: Do not output the trailing newline.-e: Enable interpretation of backslash escapes.-E: Disable interpretation of backslash escapes (default).
Variable set
var1='this is simple text': this will store a simple testdata in 'val1'var2="this is simple text $HOSTNAME": this will store a simple testdata in 'val2' also store a commandvar3="this is simple text $(date)": this will store a simple testdata in 'val3' also store a command outputvar4="this is simple text `date`": this will store a simple testdata in 'val4' also store a command outputvar5="this is simple text \$(date)": this will store a simple testdata in 'val5' also store a command output but with backslash escape
Backslash Escapes (used with -e)
\\: Backslash\a: Alert (bell)\b: Backspace\c: Suppress trailing newline\f: Form feed\n: New line\r: Carriage return\t: Horizontal tab\v: Vertical tab
Examples
1. Display a simple message
echo "Hello, World!"
Output:
Hello, World!
2. Suppress the trailing newline
echo -n "Hello, World!"
Output:
Hello, World!
3. Enable interpretation of backslash escapes
echo -e "Line1\nLine2"
Output:
Line1
Line2
4. Display a variable
name="Linux"
echo "Welcome to $name"
Output:
Welcome to Linux
5. Disable interpretation of backslash escapes
echo -E "Line1\nLine2"
Output:
Line1\nLine2
5. Color output Display
echo -e "\e[31mThis is red text\e[0m"
Output:
This is red text
some colors are:
\e[31m: Red\e[32m: Green\e[33m: Yellow\e[34m: Blue\e[35m: Magenta\e[36m: Cyan\e[0m: Reset color
6. Display a message with special characters
echo -e "This is a backslash: \\"
Output:
This is a backslash: \
7. Display a message with a tab space
echo -e "Column1\tColumn2"
Output:
Column1 Column2
8. Display a message with a bell sound
echo -e "This will ring the bell\a"
Output: This will ring the bell (you may hear a sound depending on your terminal settings).
9. Display a message with a form feed
echo -e "This is a form feed\f"
Output: This is a form feed (the output may not be visible in all terminals).
10. Display a message with a vertical tab
echo -e "This is a vertical tab\v"
Output: This is a vertical tab (the output may not be visible in all terminals).
11. Display a message with a carriage return
echo -e "This is a carriage return\rNew Line"
Output: This is a carriage return New Line (the output will overwrite the previous line).
12. Display a message with multiple lines
echo -e "Line1\nLine2\nLine3"
Output:
Line1
Line2
Line3
13. Display a message with a variable and special characters
greeting="Hello"
echo -e "$greeting, World!\nThis is a test with a tab:\tEnd of line."
Output:
Hello, World!
This is a test with a tab: End of line.
14. Display a message with a variable and color
color="\e[32m"
echo -e "${color}This is green text\e[0m"
Output:
This is green text
15. Display a message with a variable and backslash escape
path="/home/user"
echo -e "The path is: $path\nThis is a backslash: \\"
Output:
The path is: /home/user
This is a backslash: \
16. Display a message with a variable and multiple backslash escapes
message="Hello\nWorld"
echo -e "Message:\n$message\nThis is a tab:\tEnd of line."
Output:
Message:
Hello
World
This is a tab: End of line.
17. Display a message with a variable and multiple colors
color1="\e[34m"
color2="\e[31m"
echo -e "${color1}This is blue text${color2} and this is red text\e[0m"
Output:
This is blue text and this is red text
18. Display a message with a variable and special characters in a loop
for i in {1..3}; do
echo -e "Iteration $i: Hello\nWorld"
done
Output:
Iteration 1: Hello
World
Iteration 2: Hello
World
Iteration 3: Hello
World