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.
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.
Syntax:
echo "some text here" > /path/to/file
Example:
$ 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:
echo $?;
Now check the content of our file /tmp/test.txt. I'll use the cat command:
cat /tmp/test.txt
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 ">>".
Syntax:
echo "Some text to be appended" >> /path/to/file
Example:
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
cat /tmp/test.txt
To suppress the trailing newline, use the -n switch like "echo -n linux". Example:
echo -n vitux
You can use the echo command to return the value of Bahs variables like $PAT. Example
echo $PATH
This command will answer with the application search PATH of your current Linux user.
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).
Example:
ssh user@remotesystem -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.
What is the echo command?
The echo command in Linux displays lines of text or string that are passed as arguments. It is commonly used in scripting and text output operations.
How can I write text to a file using the `echo` command?
To write text to a file using echo, use the syntax:
echo "your text" > filename.txt
This will create or overwrite the file with your text.
What is the difference between > and >> when using echo?
The > operator overwrites the entire content of the file with the new content, while >> appends the new content to the end of the existing file content without deleting anything.
Can I add a new line to a file using echo?
Yes, you can add a new line by using echo followed by your text and then redirecting it to the file with >>. For example:
echo "new line of text" >> filename.txt
How do I use echo to add multiple lines to a file?
For multiple lines, you can use echo with the -e option and newline characters \n, like this:
echo -e "line 1\nline 2" > filename.txt
Is there a way to use echo to create an empty file?
Yes. you can create an empty file by using echo with an empty string:
echo "" > filename.txt
How can I combine variables with text in echo?
You can combine variables with text in `echo` by directly placing the variable in the string. For example;
echo "Value is $VARIABLE" > filename.txt
Can I use echo to write special characters into a file?
Yes, you can write special characters by using the escape character "\". For instance:
echo "Line with special character \*" > filename.txt
How do I write echo command outputs to a file in a script?
In a script, use echo just like in the command line. For example:
echo "Script output" > output.txt
Are there any limitations to what can be written to a file with echo?
echo is quite versatile but might not be suitable for complex formatting or binary data. For simple text, including special characters with escape sequences, it works well.
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…