36

I am trying to figure out what is the usage of this command:

echo < a.txt

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.

8
  • What command have you tried?
    – Yawar
    Commented Mar 13, 2014 at 11:53
  • As I said I tried : echo < a.txt
    – Bernard
    Commented Mar 13, 2014 at 11:55
  • @fedorqui So what is the usage of: echo < a.txt
    – Bernard
    Commented Mar 13, 2014 at 11:55
  • 1
    @keltar, can you explain why providing this functionality would be such a crime?
    – Andrew S
    Commented Feb 18, 2018 at 19:01
  • 1
    @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.
    – keltar
    Commented Feb 19, 2018 at 3:34

7 Answers 7

61

echo doesn't read stdin so in this case, the redirect is just meaningless.

echo "Hello" | echo

To print out a file just use the command below

echo "$(<a.txt )"

2
  • 3
    Here for future reference: in my case, echo "$(<a.txt )" works with a bash shell (bash), but not with a Bourne shell (sh) - meaning that if you want this echo "$(<a.txt )" to work, you might need to have the first line of your script as #!/bin/bash instead of #!/bin/sh. Otherwise, it might not work.
    – M.Ionut
    Commented Jun 18, 2022 at 12:24
  • 3
    Cyber Security Perspective: In restricted shells, echo "$(<flag.txt )" command might be handy to read files if it is allowed to be execute. Disclaimer: The information provided in this post is for educational purposes only and does not constitute legal advice.
    – Ertaku
    Commented Jul 15, 2023 at 6:25
11

In Unix, I believe all you have to do, assuming you have a file that isn't hefty is: cat <filename>

No echo required.

4

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.

2

I using this way to print out content of a text file:

echo 'Testing' > test.txt

And i using the cat command to see the content of file.

cat test.txt
0

use below command to print the file content using echo,

echo `cat file.txt`

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 
0

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.

For example:

xargs echo < file.txt

or

cat file.txt | xargs echo
-6

your can use

type test.txt
pause
1
  • On Windows yes, but not on Unix ?? Commented Mar 13, 2014 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.