- How to put a newline special character into a file using the echo command and redirection operator?
- 5 Answers 5
- Echo Command in Linux (With Examples)
- Echo Command Syntax
- Echo Command Options
- Examples of Echo Command
- Changing the Output Format
- Writing to a File
- Displaying Variable Values
- Displaying Command Outputs
- How to Echo Into File
- Echo Into File (echo overwrite file)
- Add more content to the file using Echo
- Using variables in echo command
- Echo into file on Remote System
- Search
- About This Site
- Latest Tutorials
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 >.
Echo Command in Linux (With Examples)
The echo command is a built-in Linux feature that prints out arguments as the standard output. echo is commonly used to display text strings or command results as messages.
In this tutorial, you will learn about all the different ways you can use the echo command in Linux.
Echo Command Syntax
The echo command in Linux is used to display a string provided by the user.
For example, use the following command to print Hello, World! as the output:
Note: Using the echo command without any option returns the provided string as the output, with no changes.
Echo Command Options
Use the —help argument to list all available echo command options:
Note: Using the echo —help command returns —help as the output.
The echo command uses the following options:
- -n : Displays the output while omitting the newline after it.
- -E : The default option, disables the interpretation of escape characters.
- -e : Enables the interpretation of the following escape characters:
- \\: Displays a backslash character (\).
- \a : Plays a sound alert when displaying the output.
- \b : Creates a backspace character, equivalent to pressing Backspace.
- \c : Omits any output following the escape character.
- \e : The escape character, equivalent to pressing Esc.
- \f : The form feed character, causes the printer to automatically advance to the start of the next page.
- \n : Adds a new line to the output.
- \r : Performs a carriage return.
- \t : Creates horizontal tab spaces.
- \v : Creates vertical tab spaces.
- \NNN : Byte with the octal value of NNN .
- \xHH : Byte with the hexadecimal value of HH .
Examples of Echo Command
Here are some ways you can use the echo command in Linux:
Changing the Output Format
Using the -e option allows you to use escape characters. These special characters make it easy to customize the output of the echo command.
For instance, using \c let you shorten the output by omitting the part of the string that follows the escape character:
echo -e 'Hello, World! \c This is PNAP!'
Note: If you are using the -e option, enter your string enclosed in single quotation marks. This ensures that any escape characters are interpreted correctly.
Use \n any time you want to move the output to a new line:
echo -e 'Hello, \nWorld, \nthis \nis \nPNAP!'
Add horizontal tab spaces by using \t :
Use \v to create vertical tab spaces:
echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
Using ANSI escape sequences lets you change the color of the output text:
echo -e '\033[1;37mWHITE' echo -e '\033[0;30mBLACK' echo -e '\033[0;31mRED' echo -e '\033[0;34mBLUE' echo -e '\033[0;32mGREEN'
Writing to a File
Use > or >> to include the string in an echo command in a file, instead of displaying it as output:
sudo echo -e 'Hello, World! \nThis is PNAP!' >> test.txt
If the specified text file doesn’t already exist, this command will create it. Use the cat command to display the content of the file:
Note: Using > overwrites the content of the text file with the new string, while >> adds the new string to the existing content.
Displaying Variable Values
The echo command is also used to display variable values as output. For instance, to display the name of the current user, use:
Displaying Command Outputs
The echo command allows you to include the result of other commands in the output:
- [string] : The string you want to include with echo
- [command] : The command you want to combine with the echo command to display the result.
For instance, list all the files and directories in the Home directory by using:
echo "This is the list of directories and files on this system: $(ls)"
After reading this tutorial, you should know how to use the echo command in Linux.
For more Linux commands, check out our Linux Command Cheat Sheet
How to Echo Into File
The Linux shell has several operators to redirect or pipe the output of commands into a file. In this guide, I will show several ways to redirect the echo output into a file. We will replace the content of a file with the echo output, then we will append text to an existing file using echo, and finally, we will echo text to a file on a remote system by SSH. All examples shown here work on any Linux distribution like Ubuntu, Debian, Linux Mint, Rocky Linux, etc.
Echo Into File (echo overwrite file)
The “>” operator is used to replace the content of a file with the text that is returned by the echo command. The text itself is wrapped in double-quotes.
echo "some text here" > /path/to/file
$ echo "Greetings from Vitux.com" > /tmp/test.txt
The command will not show any result on the shell, but of course, you can get the exit status as with any Linux shell command. The whole output is saved to the file.
To get the return value of the last executed command, in our case, the echo command, run:
Now check the content of our file /tmp/test.txt. I’ll use the cat command:
Add more content to the file using Echo
In the second example, I will add content to our file /tmp/test.txt without replacing the content. The content will get appended to the end of the file. The operator used for appending content is “>>“.
echo "Some text to be appended" >> /path/to/file
echo "More text from Vitux here" >> /tmp/test.txt
The above command appends the text “More text from Vitux here” to the file /tmp/test.txt. The test.txt file already contains the text “Greetings from Vitux.com” from our first example. Now let#s see what’s in the file. I’ll use the cat command again to show the file content on the shell
To suppress the trailing newline, use the -n switch like “echo -n linux”. Example:
Using variables in echo command
You can use the echo command to return the value of Bahs variables like $PAT. Example
This command will answer with the application search PATH of your current Linux user.
Echo into file on Remote System
Sometimes you might want to write text into a file on another Linux system. As long as both systems are connected over a LAN or the Internet, you can use SSH. The ssh command has the -f command line switch to pass commands directly by ssh and then go to the background, allowing you to enter a password (if required).
ssh [email protected] -f 'echo "Text added via SSH" >> /tmp/test.txt'
Where “user” is the username that you like to log in to the remote server or desktop, replace the word “remotesystem” with the hostname or IP address of the remote computer.
I’ve run the command on a remote system to add some text to our test.txt file. The result is:
Now you have learned how to echo text into a file on the local system and also how to do this on a remote system via SSH.
Search
About This Site
Vitux.com aims to become a Linux compendium with lots of unique and up to date tutorials.
Latest Tutorials