how to use echo command to print out content of a text file?

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

5 Answers

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 )"

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

No echo required.

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 

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.

your can use

type test.txt
pause
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like