Redirecting the content of a file to the command "echo"

319,705

Solution 1

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:

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

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


As an alternative to $(cat file), with ksh, zsh and bash, one can also do: $(<file). That's a special operator whereby the shell as opposed to cat reads the content of the file to make up the expansion. It still strips the trailing newlines and chokes on NUL bytes except in zsh. In bash, that still forks an extra process. Also note that one difference is that you won't get any error if trying to read a file of type directory that way. Also, while $(< file) is special, $(< file; other command) is not (in zsh, when not emulating other shell, that would still expand the content of the file, by running the implicit $READNULLCMD command (typically a pager)).

Solution 2

Adding to other answers, you can also try input redirection instead of cat

echo "$(<my_file.txt)"

or

echo "`<my_file.txt`"

$(...) and `...` perform the same thing, i.e command substitution. It runs commands inside of it's scope and gives the output to the shell as if it was given as an input by the user. The only difference between these two is that $(...) can be nested simply and `...` needs to be escaped to be nested.

The quotes around command substitution preserve whitespaces

Solution 3

As it was said,

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

For instance, you can do it like this with xargs (considering that you need to enable interpretation of backslash escapes):

$ cat my_file.txt | xargs echo -e

Or simply what you've asked (whithout interpretation of escapes sequences):

$ cat my_file.txt | xargs echo

Solution 4

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:
319,705

Related videos on Youtube

danielmbcn
Author by

danielmbcn

Updated on September 18, 2022

Comments

  • danielmbcn
    danielmbcn almost 2 years

    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?

    • Kevin
      Kevin over 11 years
      Why do you insist on using echo?
    • Stéphane Chazelas
      Stéphane Chazelas about 11 years
      @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.
    • neuronet
      neuronet about 8 years
      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.
  • fpmurphy
    fpmurphy almost 8 years
    Not quite. There is a subtle difference. When the back-tick form is used, a backslash retains its literal meaning except when followed by $, `, or \. The first back-tick not preceded by a backslash terminates the command. When using the $(,,,) form, all characters between the parentheses make up the command.
  • Stéphane Chazelas
    Stéphane Chazelas about 7 years
    Note however that when using xargs echo (same as xargs btw as echo is the default command), it's the echo command from the file system that will be run, not the shell's builtin echo command. Also note that xargs expects its input in a very specific format (where blanks, newlines, quotes, backslashes have special meaning). cat doesn't make much sense here as there's only one file to concatenate (same as < file.txt xargs echo. Note that Unix compliant echos do backslash escape expansion by default and output -e upon echo -e.
  • Stéphane Chazelas
    Stéphane Chazelas about 7 years
    As that command substitution is not quoted, the split+glob operator would be applied to it. For instance if my_file.txt contains *, that would print the list of file names in the current directory.
  • Stéphane Chazelas
    Stéphane Chazelas over 3 years
    `...` can be recursive but with a much more awkward syntax. You must also watch out for backslashes inside them (even inside single quotes), and " if quoted. In any case with either syntax, they should be quoted when in list context unless you want split+glob to be applied to them. Note that $(<file) is a non-standard ksh extension (also available in bash and zsh)
  • Ricardo Alves
    Ricardo Alves over 2 years
    Thank you! This is extremely helpful for debuging when your system SSD/HDD crashed and cat can't be loaded any more. For example on a Raspberry Pi that browned out due to insufficent voltage cat /sys/devices/platform/soc/soc:firmware/get_throttled yields bash: cat: command not found But the shell built-in echo `</sys/devices/platform/soc/soc:firmware/get_throttled` still works and returns the desired debug code.