- End of line (new line) escapes in Bash
- 2 Answers 2
- How to echo a New Line in Bash Shell Scripts
- Display new line with -e flag of echo command (recommended)
- echo a variable containing new line
- Use the ‘$’ character instead of -e flag
- echo your echo to print something with new line
- Use printf to print newline in Bash shell
- Conclusion
- How to Echo Newline in Bash
- Newline in Bash
- Printing Newline in Bash
- Printing Newline Using Echo
- Printing Newline Using $
- Printing Newlines with Multiple Echo Statements
- Printing Newline with Printf
- Final Thoughts
- About the author
- Sidratul Muntaha
- How can I have a newline in a string in sh?
- 13 Answers 13
- Adding newline characters to unix shell variables
- 9 Answers 9
End of line (new line) escapes in Bash
I am not trying to echo a new line. I am wondering why \ is able to new line character ( \n ) which has two character instead of just escape the backslash in the new line character and produce the literal of \n .
Hmm, still not sure I understand your question. Perhaps it will help to know that \n is not really a newline character — it is an escape sequence that represents a newline (which is just one character in Linux). The \ at the end of a line escapes the actual newline character that you type in using the enter key.
@MarkkuK, thank you! this actually answers my question. I always thought \n is the new line character it self. That’s why I don’t understand why a backslash can escape two characters. Sorry about the bad phrasing.
As a technical addendum to @MarkkuK.’s comment, you can see the newline/linefeed character is a single ASCII value ( 10 ) on an ASCII table.
2 Answers 2
Actually, \n is not really a newline character—it is an escape sequence that represents a newline (which is just one character in Linux). The \ at the end of a line escapes the actual newline character that you type in using the enter key. You can look at what ASCII values represent different characters using hexdump:
%echo $'\\n' \n %echo $'\\n' | hexdump -C 00000000 5c 6e 0a |\n.| 00000003
You will notice that echo printed out 3 characters: \ (5c), n (6e), and a newline (0a). You will also notice that on the right hand side of the hexdump output, newline shows up as a «.», because it is considered a non-printing character.
Close, but if you avoid doubling up the ` and include the -n` parameter to echo to avoid the implicit newline then you get a newline character. sh echo -n $’\n’ | hexdump -C Which results in: «` 00000000 0a |.| 00000001 «`
Newline is the name given in the UNIX world to a character that ends a line in a line-oriented file (or in a terminal). In the UNIX/Linux world this corresponds to the ASCII linefeed character.
Different systems use different conventions to end lines: Windows uses a sequence of carriage return and line feed, while Mac originally used a single carriage return. This confusion stems from the fact that these were originally commands needed to move a printer’s print head to the beginning of a new line.
\n is a conventional way of expressing the end of line character in code, again originally in the UNIX world, more precisely in the C language. Note that when reading a text file C reads a single newline character even on systems where this is really a two character sequence.
How to echo a New Line in Bash Shell Scripts
Learn various ways of displaying a new line in the output of echo command in Linux.
The echo command automatically adds a new line at the end. That’s cool.
But what if you want to display just an empty new line? Or if you want to output something that contains a new line?
The good news is that, echo lets you use the newline character \n to print a new line within the same output line if you use the -e option:
echo -e "Name\nAddress\nPhone Number"
If you run the above command, you’ll get this output:
Name Address Phone Number
That’s nice, right? Let’s have a more detailed look into it.
Display new line with -e flag of echo command (recommended)
A newline is a term we use to specify that the current line has ended and the text will continue from the line below the current one. In most UNIX-like systems, \n is used to specify a newline. It is referred to as newline character.
The echo command, by default, disables the interpretation of backslash escapes. So if you try to display a newline using the ‘\n’ escape sequence, you will notice a problem.
$ echo Hello\nworld Hellonworld $ echo 'Hello\nworld' Hello\nworld
Enclosing text in single quotes as a string literal does not work either.
That was not an expected output. To actually print a new-line, you can use the ‘-e’ flag to tell the echo command that you want to enable the interpretation of backslash escapes.
$ echo -e 'Hello\nworld' Hello world
Nice, that’s what you are looking for.
Let me some other ways to display the newline character.
echo a variable containing new line
You can store a string in a bash variable and then echo it using the ‘-e’ flag.
$ str="Hello\nworld" $ echo -e $str Hello world
Use the ‘$’ character instead of -e flag
The dollar symbol, ‘$’ is called the «expansion» character in bash. This is the character that I used in the earlier example to refer to a variable’s value in shell.
If you look closely at the snippet below, you will realize that the expansion character, in this case, acts to hold a temporary value.
$ echo Hello$'\n'world Hello world
Or, you can use the whole string as a ‘temporary variable’:
$ echo $'Hello\nworld' Hello world
I would prefer to use the -e flag, though.
echo your echo to print something with new line
When you echo a piece of text, the echo command will automatically add a newline (and here is how you can prevent it) to the end of your text.
This means that you can chain multiple echo commands together to cause a newline.
$ echo Hello; echo world Hello world
Use printf to print newline in Bash shell
printf is another command line tool that essentially prints text to the terminal, but it also allows you to format your text.
The usage is very simple and similar to echo but a bit more reliable and consistent.
$ printf 'Hello\nworld\n' Hello world
As expected, you have a newline without using any flags.
Conclusion
Personally, I would prefer sticking with the -e flag or go for the printf command for displaying the new lines in output. I recommend you to do the same but feel free to experiment.
How to Echo Newline in Bash
In Bash, there are multiple ways we can display a text in the console or terminal. We can use either the echo or printf command to print a text. Each of these commands has their unique behaviors.
In this guide, we’ll learn how to print a newline in Bash.
Newline in Bash
Before going further, here’s a quick refresh on what a newline is. It’s usually used to specify the end of a line and to jump to the next line. It’s expressed with the character “\n” in UNIX/Linux systems. Most text editors will not show it by default.
Printing Newline in Bash
There are a couple of different ways we can print a newline character. The most common way is to use the echo command. However, the printf command also works fine.
Using the backslash character for newline “\n” is the conventional way. However, it’s also possible to denote newlines using the “$” sign.
Printing Newline Using Echo
The echo command takes a string as input and prints it out on the console screen. To print any text, we use the echo command in the following manner:
As mentioned earlier, the newline character is “\n”, right? How about we try to include it directly with echo?
Well, that didn’t go as expected. What happened?
By default, the echo command will print the string provided, character by character. It doesn’t interpret backslash characters. However, we can fix this by adding the flag “-e”. It enables backslash character interpretation. Let’s fix the command and run it again:
Voila! Now it’s working as expected!
This technique also works when using Bash variables. Take a look at the following example:
$ sentence = «The \n Quick \n Brown \n Fox»
Printing Newline Using $
We can also use the “$” sign with the echo command to specify the newline character. This method is a bit more complex than the previous one. The explanation is best done with an example.
Run the following command:
- The given string isn’t inside double quotations.
- Before each newline character “\n”, we’re using the “$” sign.
- Each newline character “\n” is provided inside single quote.
Printing Newlines with Multiple Echo Statements
In this approach, we’re basically going to run multiple echo commands instead of one. By default, echo prints the given string and adds a newline character at the end. By running multiple echo statements at once, we’re taking advantage of that.
Let’s have a look at the following example.
- We’re running 4 echo commands.
- Each command is separated by a semicolon (;). It’s the default delimiter in Bash.
Printing Newline with Printf
Similar to echo, the printf command also takes a string and prints it on the console screen. It can be used as an alternative to the echo command.
Have a look at the following example.
As you can see, printf processes backslash characters by default, no need to add any additional flags. However, it doesn’t add an additional newline character at the end of the output, so we have to manually add one.
Final Thoughts
In this guide, we’ve successfully demonstrated how to print newlines in Bash. The newline character is denoted as “\n”. Using both the echo and printf commands, we can print strings with new lines in them. We can also cheat (well, technically) by running the same tool multiple times to get the desired result.
For more in-depth info about echo and printf, refer to their respective man pages.
Interested in Bash programming? Bash is a powerful scripting language that can perform wonders. Check out our Bash programming section. New to Bash programming? Get started with this simple and comprehensive guide on Bash scripting tutorials for beginners.
About the author
Sidratul Muntaha
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
How can I have a newline in a string in sh?
What should I do to have a newline in a string? Note: This question is not about echo. I’m aware of echo -e , but I’m looking for a solution that allows passing a string (which includes a newline) as an argument to other commands that do not have a similar option to interpret \n ‘s as newlines.
13 Answers 13
If you’re using Bash, you can use backslash-escapes inside of a specially-quoted $’string’ . For example, adding \n :
STR=$'Hello\nWorld' echo "$STR" # quotes are required here!
If you’re using pretty much any other shell, just insert the newline as-is in the string:
Bash recognizes a number of other backslash escape sequences in the $» string. Here is an excerpt from the Bash manual page:
Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows: \a alert (bell) \b backspace \e \E an escape character \f form feed \n new line \r carriage return \t horizontal tab \v vertical tab \\ backslash \' single quote \" double quote \nnn the eight-bit character whose value is the octal value nnn (one to three digits) \xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) \cx a control-x character The expanded result is single-quoted, as if the dollar sign had not been present. A double-quoted string preceded by a dollar sign ($"string") will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.
Adding newline characters to unix shell variables
I have a variable in a shell script in which I’d like to format the data. The variable stores new data during every iteration of a loop. Each time the new data is stored, I’d like to insert a new line character. Here is how I’m trying to store the data into the variable. VARIABLE=»$VARIABLE ‘\n’ SomeData» Unfortunately, the output includes the literal ‘\n’ Any help would be appreciative.
9 Answers 9
Thanks vmpstr. I may be mistaken, but I believe VAR=$VAR$’\n’b must be surrounded by double quotes. Which results in a literal ‘\n’.
You can switch between different kinds of quotes in the same «word»: VAR=»$VAR»$’\n'»some data» has $VAR in double-quotes, \n in $» so it’ll get interpreted as a newline, and then some data back in double-quotes. As long as there’s no space between them, they’ll all be concatenated together and the result assigned to VAR.
The output of echo is different than how the values are being stored in VARIABLE. The contents of VARIABLE are passed to an email util. I am not echoing the data to command line. Sorry for the confusion.
I got it! Turns out I had some spacing issues. Thanks to everyone who cared to comment, especially Gordon Davisson. Here was my issue: Original Code: VAR=»$VAR»$’\n'»Audit Source: $B Audit Path: $C» The spaces for formatting was messing things up. New Code: VAR=»$VAR»$’\n'»Audit Source:$B»$’\t'»Audit Path:$C» No spaces here! Anyone know why the spacing causes issues? Even though the Original Code has everything surrounded by «?