The echo
command is used to display a line of text or string that is passed as an argument. It's one of the most basic and frequently used commands in Bash scripting.
echo [option] [string]
Printing text to the terminal:
echo "Hello, World!"
Output:
Hello, World!
Printing the value of a variable:
NAME="Alice"
echo "Hello, $NAME!"
Output:
Hello, Alice!
Using escape sequences:
echo -e "Line 1\nLine 2"
Output:
Line 1
Line 2
Suppressing the trailing newline:
echo -n "Hello, World!"
Example: print the following text exactly as it is, including the double quotes and a tab space between the two lines
"This is line 1" "This is line 2"
Answer: echo -e "\"This is line 1\"\t\"This is line 2\""