To print multiple lines using echo on the command line in Bash, you can use the -e option to enable the interpretation of backslash escape sequences, specifically \n for a newline character.
Here are the methods:
1. Using \n with echo -e:
This is the most common and straightforward method. You can embed \n within a double-quoted string to introduce newlines.
Code
echo -e "This is the first line.\nThis is the second line.\nAnd this is the third line."
2. Using multi-line input with double quotes:
Bash allows you to continue a double-quoted string across multiple lines directly on the command line. When you press Enter after an opening double quote, the shell will provide a secondary prompt (often >) until you close the quote.
Code
echo "This is the first line.
> This is the second line.
> And this is the third line."
3. Using printf:
The printf command offers more control over formatting and is often preferred for more complex output, including multi-line strings.
Code
printf "This is the first line.\nThis is the second line.\nAnd this is the third line.\n"
Note that printf does not automatically add a newline at the end, so you need to include \n explicitly if desired.