Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

$ cat my_file.txt | echo

and also this:

$ echo < my_file.txt

But in both cases it appears only a blank in the stdout, not the content of my_file.txt.

How could I do that?

Thanks in advance :-)

share|improve this question
Why do you insist on using echo? – Kevin Feb 4 '13 at 14:53
@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. – Stéphane Chazelas Apr 2 '13 at 6:38
up vote 9 down vote accepted

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

If you want echo to display the content of a file, you have to pass that content as an argument to echo. Something like:

echo "$(cat my_file.txt)"

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.

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:

printf '%b\n' "$(cat my_file.txt)"

Contrary to echo, that one is portable and won't have problems if the content of the file starts with -.

share|improve this answer
In both bash and zsh you can skip the cat and just have the shell read it in directly: echo "$(<my_file.txt)". – Kevin Feb 4 '13 at 14:52
Yes, with ksh, zsh and bash you can use $(<filename). – Dimitre Radoulov Feb 4 '13 at 15:25
Thank you so much, that's what I needed and it worked! :-) – danielmbcn Feb 4 '13 at 15:36

Simple answer: you can't. echo (be it shell built-in or regular binary) doesn't process its standard input - it is one way only.

share|improve this answer
Oh... okay, thank you. – danielmbcn Feb 4 '13 at 14:33
Question is, why would you actually want to do this? – peterph Feb 4 '13 at 19:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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