Tipăriți Newline în Bash

Introducere

In Bash, a linie nouă refers to the end of a line of text and the beginning of a new one. When a command is executed in Bash, the output is often displayed on the terminal with each line of text ending in a new line. The new line character is represented by a special escape sequence, n, which tells the terminal to move the cursor to the beginning of the next line.

Printing a new line in Bash is important because it helps to format the output in a way that is easy to read and understand. For example, if you are printing the results of a command that produces a lot of output, using new lines can make it easier to parse the information. Additionally, new lines can be used to visually separate different sections of output, making it easier to locate specific information.

In this article, we’ll take a a look at several different ways to print a new line, including using the echo comanda cu -e opțiune, printf command, and the escape sequence n.

Two Ways to Print a New Line in Bash

Here are several different ways to print a new line in Bash. In this section we’ll take a look at two most common.

Utilizarea ecou comanda cu "-la" opțiune

echo command is used to display a message or output on the Bash. It is often used to print a string of text, but it can also be used to print the value of a variable or the output of a command.

echo command has a number of options that allow you to customize its behavior. The one we are interested in is the -e option, which allows you to use escape sequences to include special characters in the output.

That specific option enables us to use the n special character to print a new line in Bash:

echo -e "This is the first linenThis is the second line"

Aceasta ne va oferi:

This is the first line
This is the second line

Utilizarea printf comandă

In Bash, the printf command is used to display a message or output in the terminal. It is similar to the echo command, but it is generally more powerful and flexible, and it is often preferred over echo for formatting and printing output in Bash scripts.

Aprecieri echo, printf can be used to print a string of text, the value of a variable, or the output of a command. However, printf vă permite, de asemenea specify a format string, which allows you to control the appearance of the output in more detail.

Therefore, there is no need to use any special options/flags (like -e), just write the stirng format between two quotation marks:

printf "This is the first linenThis is the second line"

As expected, this will yield us with:

This is the first line
This is the second line

Notă: In all of these examples, the n escape sequence is used to represent a new line. The echo și printf commands are used to print the text to the terminal, and the -e opțiune pentru echo allows it to interpret the escape sequences.

Concluzie

In conclusion, writing a new line in Bash is an important task that allows you to format the output of your scripts and commands in a way that is easy to read and understand. In this article, we covered two most common ways to write a new line in Bash – using the echo comanda cu -e opțiunea și printf comanda.

Timestamp-ul:

Mai mult de la Stackabuse