An “echo” is the Linux built-in command line tool that displays or prints the text string in the terminal passed as an argument. The text strings can be displayed in different styles, such as removing spaces, creating vertical/horizontal tab spaces, adding/omitting new lines, and many others.
Apart from its basic functionality, it can also be utilized with various parameters to perform some other useful operations from which “add new lines in the output” is one of them.
This post lists the different ways to add new lines while using the “echo” command in Linux.
- Method 1: Using “-e” Backspace Interpreter
- Method 2: Using “$” Bash Variable
- Alternative Method 1: Using “Multiple “echo” Statements
- Alternative Method 2: Using the “printf” Command
Method 1: Using “-e” Argument
The “echo” command offers the “-e” backspace interpreter to print each string of a statement in a new line.
Execute the “echo” command with the “-e” interpreter and put the “\n” after each string/pattern in this format to perform this task:
$ echo -e "Welcome\nto\nitslinuxfoss\nWebsite"
The command has executed successfully and each string of the defined statement as printed in the new line.
The “-e” interpreter can also be used with the “$” bash variable for adding new line characters. Let’s see its practical implementation.
- First, assign the statement that the user wants to print to a variable, i.e., “statement”.\
- Execute the “echo” command with “-e” flag and just pass the “$statement” variable as an argument:
$ statement="Linux\nis\nan\nwell-known\noperating\nsystem" $ echo -e $statement
The same output is displayed in the terminal, i.e., the new line is added.
Method 2: Using “$” Bash Variable
The “$” is the bash variable that is the temporary storage of the string/number. In this scenario, use the “$” variable after each string to add new lines through the “echo” command:
$ echo Linux$'\n'is$'\n'a$'\n'free$'\n'OS
There is no need to enclose the whole statement in “(Double quotes). However, the “\n” character must be coated in single quotes like this:
The “\n” line character. i.e., a new line is added before each string successfully.
Alternative Method 1: Using “Multiple “echo” Statements
By default, the “echo” command adds a new line character at the end of the statement.
However, it can also be utilized more than one time in a single command before each string to add a new line character after it:
$ echo Ubuntu; echo Fedora; echo CentOS; echo RHEL
The output shows a newline character has been added after every string of the statement.
Alternative Method 2: Using the “printf” Command
The “printf” is an alternative to the “echo” command and can also be used to add a new line. It is pre-installed in all Linux distributions.
Specify the “\n(newline)” with the “printf” command after every string of the statement and enclosed in the whole statement in double quotes:
$ printf "Welcome\nto\nitslinuxfoss\nWebsite\n"
The newline character has been successfully added.
The “echo” command can easily add newlines by using “-e(backspace interpreter)” and “$(bash variable)”. The “echo” command itself can be used multiple times in a single command to add new lines without using any additional flags or parameters. In addition, the user can also use the “printf” command which is an alternative to “echo”.
This post has illustrated all possible aspects to add new lines using the echo command.