- How to put a newline special character into a file using the echo command and redirection operator?
- 5 Answers 5
- 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
- echo text with new line in bash
- 3 Answers 3
- How to insert a new line in Linux shell script? [duplicate]
- 4 Answers 4
How to put a newline special character into a file using the echo command and redirection operator?
I would like to create a file by using the echo command and the redirection operator, the file should be made of a few lines. I tried to include a newline by «\n» inside the string:
echo "first line\nsecond line\nthirdline\n" > foo
but this way no file with three lines is created but a file with only one line and the verbatim content of the string. How can I create using only this command a file with several lines ?
5 Answers 5
You asked for using some syntax with the echo command:
echo $'first line\nsecond line\nthirdline' > foo
(But consider also the other answer you got.)
The $’. ‘ construct expands embedded ANSI escape sequences.
@ChrisDown, it’s not POSIX yet, but scheduled for inclusion in issue 8. ksh93, bash, zsh, mksh, FreeBSD sh support it.
What echo does with character escapes is implementation defined. In many implementations of echo (including most modern ones), the string passed is not examined for escapes at all by default.
With the echo provided by GNU bash (as a builtin), and some other echo variants, you can do something like the following:
echo -en 'first line\nsecond line\nthird line\n' > file
However, it really sounds like you want printf , which is more legible to my eye, and more portable too (it has this feature defined by POSIX):
printf '%s\n' 'first line' 'second line' 'third line' > file
You also might consider using a here document:
i tried your cat > file << 'EOF' in the shell, an when i hit enter, i got this >shaped prompt with each line, but i could not get out of it even by hitting ctrl+x or ctrl+z<>
interesting, though I do not know how this structure works, first time i met it, i tried ‘ficken’ instead of ‘EOF’ and it works, too , but i have to use the other word than. cat > myfile
@AbdulAlHazred as the link in the answer indicates, it’s called a «here document» and is very common. Wikipedia is good on this topic: en.wikipedia.org/wiki/Here_document .
This worked for my Mac OSX (specifically echo -en ‘text..’ , whereas the accepted answer didn’t recognize my newline character ( \n ), the echo -en did.
Here are some other ways to create a multi-line file using the echo command:
echo "first line" > foo echo "second line" >> foo echo "third line" >> foo
where the second and third commands use the >> redirection operator, which causes the output of the command to be appended (added) to the file (which should already exist, by this point).
(echo "first line"; echo "second line"; echo "third line") > foo
where the parentheses group the echo commands into one sub-process, which looks and acts like any single program that outputs multiple lines (like ls , for example).
A subtle variation on the above is
This is slightly more efficient than the second answer in that it doesn’t create a sub-process. However, the syntax is slightly trickier: note that you must have a space after the < and a semicolon before the >.
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.
echo text with new line in bash
echo by default does leave a newline — do you need it to leave two? Also, if you’re running this on a linux system and opening the file on a windows or mac system, make sure your editor supports *nix newlines, or it’ll appear all on one line even though it’s on multiple lines.
3 Answers 3
% ( echo text ; echo "" ) >> file.conf
% echo text >> file.conf % echo "" >> file.conf
You can drop the empty argument ( echo «» => echo ). Also, option 4: echo $’text\n’ , which is useful for any command as the $» is interpreted by bash.
I think the proper answer should be that your command
does add an extra line, but after the new text, not before.
I guess that you want to add an extra line before that text, probably because your initial file doesn’t end in a new line. In that case you could use
instead, as the -e option allows you to use the new line \n character.
Just to add to akira’s response
use ctrl-v ctrl-m key combos twice to insert two newline control character in the terminal. Ctrl-v lets you insert control characters into the terminal. You could use the enter or return key instead of the ctrol-m if you like. It inserts the same thing.
This ends up looking like echo text^M^M >> file.conf
If you need linefeeds instead of carriage returns (linux/unix), try echo «text^J^J» >> file.conf Typing ^J might actually insert a literal newline, just make sure to put the quotes and it’ll be good.
How to insert a new line in Linux shell script? [duplicate]
I want to insert a new line between multiple echo statements. I have tried echo «hello\n» , but it is not working. It is printing \n . I want the desired output like this:
Create the snapshots Snapshot created
4 Answers 4
The simplest way to insert a new line between echo statements is to insert an echo without arguments, for example:
echo Create the snapshots echo echo Snapshot created
That is, echo without any arguments will print a blank line.
Another alternative to use a single echo statement with the -e flag and embedded newline characters \n :
echo -e "Create the snapshots\n\nSnapshot created"
However, this is not portable, as the -e flag doesn’t work consistently in all systems. A better way if you really want to do this is using printf :
printf "Create the snapshots\n\nSnapshot created\n"
This works more reliably in many systems, though it’s not POSIX compliant. Notice that you must manually add a \n at the end, as printf doesn’t append a newline automatically as echo does.
but what would be the equivalent for echo «i want to be on one line» echo «i want to be on another» by using one echo command, and just \n, so that when you have long sentences, you can wrap your text in bash(using maybe 3 or 4 bash lines atleast with only one echo command. I came here hoping to find that answer. e.g. elaborate your final answer to make it multiple lines with one echo command, what if i wanted to cut it off right at Snapshot created on my bash file and make that a new bash line, without adding echo command? I think use plus + somehow right?
@blamb you can break a line using « at the end of a line. But that’s a very different topic from what was asked here.