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" > /tmp/test.txt

Redirect echo output to file - echo overwrite file

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

File content

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

Append Echo to File

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” 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

Echo append to file

To suppress the trailing newline, use the -n switch like “echo -n linux”. Example:

echo -n vitux

Using variables in echo command

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.

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 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:

Echo into file by SSH

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.