- Redirecting the content of a file to the command «echo»
- 6 Answers 6
- 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 use echo command to print out content of a text file?
- 6 Answers 6
- echo Command in Linux: 7 Practical Examples
- Examples of the echo command in Linux
- 1. Displaying the value of a variable
- 2. Don’t print the final newline character
- 3. Redirect the output to a file
- 4. Using escape characters with echo command
- 5. Display text with single or double quotes with echo command
- 6. Display files in directory
- 7. Use echo command to empty a file in Linux
- Bonus Tips
- Display echo command output in color
- See what a command will do without running it
- What else?
Redirecting the content of a file to the command «echo»
I have a file named my_file.txt whose content is just the string Hello . How could I redirect its content to the command echo ? I know I have the commands less , cat , more . but I need to do it with echo . I tried this:
But in both cases it appears only a blank in the stdout, not the content of my_file.txt. How could I do that?
@Kevin, presumably eventually he wants to expand the ANSI C escape sequences ( \n , b . ) in the file, which would be a valid usage of echo (at least a standard echo ). Or presumably he wants to understand why it doesn’t work that way. In any case, no reason to downvote IMO.
I came here because I was curious about why it doesn’t work when I do echo < file.txt. Seems a reasonable question to have at SE, the answer was very helpful. +1 from me.
6 Answers 6
You can redirect all you want to echo but it won’t do anything with it. echo doesn’t read its standard input. All it does is write to standard output its arguments separated by a space character and terminated by a newline character (and with some echo implementations with some escape sequences in them expanded and/or arguments starting with — possibly treated as options).
If you want echo to display the content of a file, you have to pass that content as an argument to echo . Something like:
Note that $(. ) strips the trailing newline characters from the output of that cat command, and echo adds one back.
Also note that except with zsh , you can’t pass NUL characters in the arguments of a command, so that above will typically not work with binary files. yash will also remove bytes that don’t form part of valid characters.
If the reason for wanting to do that is because you want echo to expand the \n , \b , \0351 . escape sequences in the file (as UNIX conformant echo implementations do, but not all), then you’d rather use printf instead:
Contrary to echo , that one is portable and won’t have problems if the content of the file starts with — .
As an alternative to $(cat file) , with ksh , zsh and bash , one can also do: $(
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
How to use echo command to print out content of a text file?
According to text book it should redirect a programs standards input. Now I am redirecting a.txt to echo but instead of printing the content of the file it is printing out one empty line! Appreciate if anyone display this behaviour.
@AndrewS but that’s just part of it, another part is I don’t really think it is possible. How would you distinguish echo foo to output «foo» but echo (empty variable, no value) to just wait for user input through stdin? That’ll probably break like a half of existing scripts.
6 Answers 6
echo doesn’t read stdin so in this case, the redirect is just meaningless.
To print out a file just use the command below
Here for future reference: in my case, echo «$(
In Unix, I believe all you have to do, assuming you have a file that isn’t hefty is: cat
use below command to print the file content using echo,
here you can also get benefit of all echo features, I most like the removing of trailing newline character, (to get exact same hash as that of buffer and not the file)
echo -n `cat file.txt` | sha256sum
cat command will display the file with CR or return:
$ cat names.txt Homer Marge Bart Lisa Maggie
you could use echo command with cat as command substitution. However, it will replace CR or return (unix: \n) with spaces:
$ echo $(cat names.txt) Homer Marge Bart Lisa Maggie
Could be an interesting feature if you want to pipe to further data processing though. E.g. replacing spaces with sed command.
The echo command does not accept data from standard input ( STDIN ), but only works on the arguments passed to it.
So if we pass data to echo from standard input, e.g. with < or | , it will be ignored because echo only works with arguments.
This can be changed by using echo together with the xargs command, which is designed to call a command with arguments that are data from standard input.
echo Command in Linux: 7 Practical Examples
Learn to use the echo command in Linux with these simple but useful examples. The echo command is useful for displaying information in bash shell scripts.
The echo command is perhaps one of the first few commands you see when you start learning Linux commands or bash shell scripting.
Echo is a simple command that simply prints its arguments on display.
[email protected]:~$ echo Hello World Hello World
You can guess why this command is called ‘echo’. Like the sound echo, the command also simply prints what it receives in the input.
Examples of the echo command in Linux
Echo is often used in shell scripts to display information, such as asking for the user to provide input or displaying the error or warning for a certain action in the script.
The echo command has a simple syntax with a few options:
echo [options] [input string]
Let’s see some examples of the echo command.
1. Displaying the value of a variable
Let’s say you declared a variable var = 100. You can display the value of this variable using the echo command:
echo The value of variable var is $var
Its output would be displayed as this:
The value of variable var is 100
2. Don’t print the final newline character
By default, the output of the echo command adds a new line to the output so that you have the output displayed in one entire line.
[email protected]:~$ echo Hello World Hello World
You can suppress this new line character using the option -n:
[email protected]:~$ echo -n Hello World Hello [email protected]:~$
3. Redirect the output to a file
You can use echo command to create a new file in Linux or append new text to an existing file by redirecting the output to a file:
echo "This text goes to a file" >> file.txt
4. Using escape characters with echo command
The option -e with echo allows you to interpret the escape characters. With escape characters, you can display your text on the screen in a different manner of your choice.
The following escape characters are available with escape:
- \a – alert (plays a beep sound)
- \b – backspace
- \c – don’t print the final newline (same as -n option)
- \f – from feed
- \n – newline
- \r – carriage return
- \t – horizontal tab
- \v – vertical tab
- \\ – backslash
- \’ – single quote
- \” – double quote
While -e option asks the echo command to interpret the escape characters, the -E option does the opposite and ignores the escape characters.
Now let’s see how to use some of these escape characters with echo command.
echo -e "An eye for an eye leaves the whole world blind\n\t- Gandhi"
Can you guess what would be the output of the above command? Here it is:
An eye for an eye leaves the whole world blind – Gandhi
Let’s see some other examples of escape characters. The carriage return character prints only the text after that character.
If you use the backspace character, it will remove one character before it:
[email protected]:~$ echo -e “Hello \bWorld” HelloWorld
You can play with other escape characters in the similar way.
5. Display text with single or double quotes with echo command
Dealing with single quotes and double quotes is often a headache.
If you have a single quote in the text that you want to display, you should wrap the entire input within double quotes:
[email protected]:~$ echo “It’s My World” It’s My World
If you want to display the double quotes, you have to ‘escape’ it:
[email protected]:~$ echo “It’s \”My\” World” It’s “My” World
6. Display files in directory
You probably use the ls command to display the files in a directory. You can also use echo command for this purpose.
To display the contents of the present directory, you can use this command:
You can also use echo command to display only a certain type of files:
If you want to list only the directories, use it like this:
You cannot view the file content with echo, unfortunately.
7. Use echo command to empty a file in Linux
I have already shown this tip for emptying log files in Linux. If you have a text file and you want to clear its content without deleting the file itself, you can use the echo command in the following fashion:
Bonus Tips
Here are a few bonus tip to use the echo command in unusual or colorful manner.
Display echo command output in color
You probably would have come across some bash script that displays the text output in different color. Have you ever wondered how it is done? Let me quickly show you how to change the color of echo output.
You can use the ANSI escape sequence and change the appearance of the output like background color, underscore, bold etc.
To change the color of the output text, you can use these:
echo -e “\033[30mThis is Black”
echo -e “\033[31mThis is Red”
echo -e “\033[32mThis is Green”
echo -e “\033[33mThis is Yellow”
echo -e “\033[34mThis is Blue”
echo -e “\033[35mThis is Magenta”
echo -e “\033[36mThis is Cyan”
echo -e “\033[37mThis is White”
Sample colored output for your understanding:
\033[ represents ESC[ in the above commands.
See what a command will do without running it
In many cases, the echo command saves you the trouble of accidentally doing something you didn’t expect.
It allows you to expand the wildcards to understand what will happen before you actually run the command.
This will output what will be passed to the rm command (and therefore what would be deleted), putting echo before a command renders it harmless (it just expands wildcards so you know what it will do).
What else?
The echo command is good for quickly printing desired output on the screen. If you want a more complicated formatted output, use the C-styled printf command in bash.
Well, that’s it. I think you have seen a good number of examples of the echo command in Linux. If you use echo for some specific purpose that could be useful to others, please share it with us in the comment section.