Bernard
Bernard

Reputation: 4600

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.

Upvotes: 38

Views: 177728

Answers (7)

Tiago Ribeiro Santos
Tiago Ribeiro Santos

Reputation: 31

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

Upvotes: 2

simhumileco
simhumileco

Reputation: 34653

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

Upvotes: 0

yoabau
yoabau

Reputation: 61

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.

Upvotes: 4

SuperHarmony910
SuperHarmony910

Reputation: 197

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

No echo required.

Upvotes: 11

Prashant Adlinge
Prashant Adlinge

Reputation: 199

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 

Upvotes: 0

hostmaster
hostmaster

Reputation: 2190

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

Upvotes: 66

user3415023
user3415023

Reputation:

your can use

type test.txt
pause

Upvotes: -6

Related Questions