Introduction
In Bash, a new line refers back to the finish of a line of textual content and the start of a brand new one. When a command is executed in Bash, the output is usually displayed on the terminal with every line of textual content ending in a brand new line. The brand new line character is represented by a particular escape sequence, n
, which tells the terminal to maneuver the cursor to the start of the subsequent line.
Printing a brand new line in Bash is vital as a result of it helps to format the output in a manner that’s simple to learn and perceive. For instance, if you’re printing the outcomes of a command that produces loads of output, utilizing new strains could make it simpler to parse the knowledge. Moreover, new strains can be utilized to visually separate completely different sections of output, making it simpler to find particular info.
On this article, we’ll take a a have a look at a number of other ways to print a brand new line, together with utilizing the
echo
command with the-e
possibility, theprintf
command, and the escape sequencen
.
Two Methods to Print a New Line in Bash
Listed here are a number of other ways to print a brand new line in Bash. On this part we’ll check out two most typical.
Utilizing the echo command with the “-e” possibility
The echo
command is used to show a message or output on the Bash. It’s typically used to print a string of textual content, but it surely may also be used to print the worth of a variable or the output of a command.
The echo
command has a lot of choices that help you customise its conduct. The one we’re considering is the -e
possibility, which lets you use escape sequences to incorporate particular characters within the output.
That particular possibility permits us to make use of the n
particular character to print a brand new line in Bash:
echo -e "That is the primary linenThis is the second line"
This can give us:
That is the primary line
That is the second line
Utilizing the printf command
In Bash, the printf
command is used to show a message or output within the terminal. It’s much like the echo
command, however it’s usually extra highly effective and versatile, and it’s typically most popular over echo
for formatting and printing output in Bash scripts.
Like echo
, printf
can be utilized to print a string of textual content, the worth of a variable, or the output of a command. Nevertheless, printf
additionally lets you specify a format string, which lets you management the looks of the output in additional element.
Due to this fact, there is no such thing as a want to make use of any particular choices/flags (like -e
), simply write the stirng format between two citation marks:
printf "That is the primary linenThis is the second line"
As anticipated, this may yield us with:
That is the primary line
That is the second line
Observe: In all of those examples, the n
escape sequence is used to symbolize a brand new line. The echo
and printf
instructions are used to print the textual content to the terminal, and the -e
possibility for echo
permits it to interpret the escape sequences.
Conclusion
In conclusion, writing a brand new line in Bash is a crucial job that lets you format the output of your scripts and instructions in a manner that’s simple to learn and perceive. On this article, we coated two most typical methods to write down a brand new line in Bash – utilizing the echo
command with the -e
possibility, and the printf
command.