Send string to stdin

205,576

Solution 1

You can use one-line heredoc

cat <<< "This is coming from the stdin"

the above is the same as

cat <<EOF
This is coming from the stdin
EOF

or you can redirect output from a command, like

diff <(ls /bin) <(ls /usr/bin)

or you can read as

while read line
do
   echo =$line=
done < some_file

or simply

echo something | read param

Solution 2

You were close

/my/bash/script <<< 'This string will be sent to stdin.'

For multiline input, here-docs are suited:

/my/bash/script <<STDIN -o other --options
line 1
line 2
STDIN

Edit To the comments:

To achieve binary input, say

xxd -r -p <<BINARY | iconv -f UCS-4BE -t UTF-8 | /my/bash/script
0000 79c1 0000 306f 0000 3061 0000 3093 0000 3077 0000 3093 0000 304b 0000 3093 0000 3077 0000 3093 0000 306a 0000 8a71 0000 306b 0000 30ca 0000 30f3 0000 30bb
0000 30f3 0000 30b9 0000 3092 0000 7ffb 0000 8a33 0000 3059 0000 308b 0000 3053 0000 3068 0000 304c 0000 3067 0000 304d 0000 000a
BINARY

If you substitute cat for /my/bash/script (or indeed drop the last pipe), this prints:

私はちんぷんかんぷんな話にナンセンスを翻訳することができ

Or, if you wanted something a little more geeky:

0000000: 0000 0000 bef9 0e3c 59f8 8e3c 0a71 d63c  .......<Y..<.q.<
0000010: c6f2 0e3d 3eaa 323d 3a5e 563d 090e 7a3d  ...=>.2=:^V=..z=
0000020: 7bdc 8e3d 2aaf a03d b67e b23d c74a c43d  {..=*..=.~.=.J.=
0000030: 0513 d63d 16d7 e73d a296 f93d a8a8 053e  ...=...=...=...>
0000040: 6583 0e3e 5a5b 173e 5b30 203e 3d02 293e  e..>Z[.>[0 >=.)>
0000050: d4d0 313e f39b 3a3e 6f63 433e 1c27 4c3e  ..1>..:>ocC>.'L>
0000060: cde6 543e 59a2 5d3e 9259 663e 4d0c 6f3e  ..T>Y.]>.Yf>M.o>
0000070: 60ba 773e cf31 803e ee83 843e 78d3 883e  `.w>.1.>...>x..>
0000080: 5720 8d3e 766a 913e beb1 953e 1cf6 993e  W .>vj.>...>...>
0000090: 7a37 9e3e c275 a23e dfb0 a63e bce8 aa3e  z7.>.u.>...>...>
00000a0: 441d af3e 624e b33e 017c b73e 0ca6 bb3e  D..>bN.>.|.>...>
00000b0: 6fcc bf3e 15ef c33e e90d c83e d728 cc3e  o..>...>...>.(.>
00000c0: c93f d03e ac52 d43e 6c61 d83e f36b dc3e  .?.>.R.>la.>.k.>
00000d0: 2f72 e03e 0a74 e43e 7171 e83e 506a ec3e  /r.>.t.>qq.>Pj.>
00000e0: 945e f03e 274e f43e f738 f83e f11e fc3e  .^.>'N.>.8.>...>
00000f0: 0000 003f 09ee 013f 89d9 033f 77c2 053f  ...?...?...?w..?
0000100: caa8 073f 788c 093f 776d 0b3f be4b 0d3f  ...?x..?wm.?.K.?
0000110: 4427 0f3f 0000 113f e8d5 123f f3a8 143f  D'.?...?...?...?
0000120: 1879 163f 4e46 183f 8d10 1a3f cad7 1b3f  .y.?NF.?...?...?
0000130: fe9b 1d3f 1f5d 1f3f 241b 213f 06d6 223f  ...?.].?$.!?.."?
0000140: bb8d 243f 3a42 263f 7cf3 273f 78a1 293f  ..$?:B&?|.'?x.)?
0000150: 254c 2b3f 7bf3 2c3f 7297 2e3f 0138 303f  %L+?{.,?r..?.80?
0000160: 22d5 313f ca6e 333f                      ".1?.n3?

Which is the sines of the first 90 degrees in 4byte binary floats

Solution 3

Solution

You want to (1) create stdout output in one process (like echo '…') and (2) redirect that output to stdin input of another process but (3) without the use of the bash pipe mechanism. Here's a solution that matches all three conditions:

/my/bash/script < <(echo 'This string will be sent to stdin.')

The < is normal input redirection for stdin. The <(…) is bash process substitution. Roughly it creates a /dev/fd/… file with the output of the substituting command and passes that filename in place of the <(…), resulting here for example in script < /dev/fd/123. For details, see this answer.

Comparison with other solutions

  • A one-line heredoc sent to stdin script <<< 'string' only allows to send static strings, not the output of other commands.

  • Process substitution alone, such as in diff <(ls /bin) <(ls /usr/bin), does not send anything to stdin. Instead, the process output is saved into a file, and its path is passed as a command line argument. For the above example, this is equivalent to diff /dev/fd/10 /dev/fd/11, a command where diff receives no input from stdin.

Use cases

I like that, unlike the pipe mechanism, the < <(…) mechanism allows to put the command first and all input after it, as is the standard for input from command line options.

However, beyond commandline aesthetics, there are some cases where a pipe mechanism cannot be used. For example, when a certain command has to be provided as argument to another command, such as in this example with sshpass.

Solution 4

You can also use read like this

echo "enter your name"
read name
echo $name
Share:
205,576

Related videos on Youtube

Uyghur Lives Matter
Author by

Uyghur Lives Matter

Recall the 1936 Olympics. Never Again. Free Tibet. Free Hong Kong. Free the Uyghurs.

Updated on August 08, 2020

Comments

  • Uyghur Lives Matter
    Uyghur Lives Matter almost 4 years

    Is there a way to effectively do this in bash:

    /my/bash/script < echo 'This string will be sent to stdin.'
    

    I'm aware that I could pipe the output from the echo such as this:

    echo 'This string will be piped to stdin.' | /my/bash/script
    
    • Andy
      Andy almost 13 years
      What do you mean by effectively, why isn't pipe a way to do this?
    • Flimzy
      Flimzy almost 13 years
      Both of those commands you listed should do exactly the same thing. What problem are you having?
    • Andy
      Andy almost 13 years
      @Flimzy: afaik, the first one won't work as expected, it'll try to open a file called echo
    • Flimzy
      Flimzy almost 13 years
      Oh right. So what's wrong with the second form?
    • sehe
      sehe almost 13 years
      @Flimzy: besides, the > redirection form will open a file descriptor as fd 0 (stdin), whereas | opens starts a child process and attaches it's stdout (fd 1) to the other process's stdin. This fact can have non-trivial consequences (think about sharing environment variables?)
  • cprcrack
    cprcrack over 11 years
    And how to input binary values in this way? Without using the pipe (I know it can be done with the pipe as follows: echo -e "\x01\x02..." | ./script)
  • cprcrack
    cprcrack over 11 years
    And how to input binary values in this way?
  • sehe
    sehe over 11 years
    I'd use xxd -r (or od) to filter the HERE document
  • cprcrack
    cprcrack over 11 years
    Could you explain it with an example? I didn't manage to do it.
  • sehe
    sehe over 11 years
    see edited answer. The point is to not include raw binary data in your script, but instead filter it through a program like xxd
  • Pyrolistical
    Pyrolistical over 10 years
    I like writing it the other way: <<< "This is coming from stdin" perl ..., this way it looks like the left hand side of the pipe.
  • clt60
    clt60 over 10 years
    @Pyrolistical yes! Especially useful when mastering for example a perl oneliner from a command line - easy editing of perl's arguments without the need much backward movements with a cursor. :)
  • Pyrolistical
    Pyrolistical over 10 years
    @jm666 sad thing is I learned yesterday if you use a bash alias, you just use the form command_alias <<< "stdin string", otherwise it will say command not found
  • ed9w2in6
    ed9w2in6 about 10 years
    What is the point of doing: "/my/bash/script <<< 'This string will be sent to stdin.'" when you can directly say: "/my/bash/script 'This string will be sent to stdin.'" ?
  • sehe
    sehe about 10 years
    @BaiyanHuang because the latter would be a lie! It's /my/bash/script 'This will be a program argument'. You use stdin if (a) the script requires it (b) argument list is too small (c) you need to working in streaming mode (processing the first data while the rest is still arriving) (d) you don't want the arguments visible in the process list
  • PT Huynh
    PT Huynh almost 8 years
    We can do like /my/bash/script -o other --options <<STDIN
  • wpcarro
    wpcarro almost 8 years
    @jm666 Is there somewhere I can learn more about <<<? I'm ashamed to admit that I've never seen it before, and I'm totally mystified by it. I tried Googling for it, but my results have been unrelated.
  • clt60
    clt60 over 7 years
    @wcarroll one of sites where you can learn more about the here strings (aka <<<) is this one tldp.org/LDP/abs/html/x17837.html
  • Uyghur Lives Matter
    Uyghur Lives Matter almost 4 years
    Interesting alternative. Does this happen to be POSIX compliant unlike <<<?
  • Leonardo Dagnino
    Leonardo Dagnino almost 4 years
    Firstly, this does not answer the question - maybe you answered the wrong question? Secondly, why would you ever define a function inside an alias? You should just do function oline() { sed -e ":a;N;\$!ba ;s?\n? ?g"; }. Thirdly, this sed would be a lot more understandable with awk: awk -F '\n' 'ORS=" " { print } END { printf "\n"}'
  • geek-merlin
    geek-merlin about 3 years
    < < (...) saved my day!
  • mekb
    mekb almost 3 years
    a bit offtopic, but what's the difference between cat<<<text and echo text or printf text
  • clt60
    clt60 almost 3 years
    @mekb takling about your examples / nothing. But imagine sed 's/x/y/' <<<'text'. Now the sed reading from its stdin the word text and outputs teyt. So, now here IS a difference. The exmple commands in the answer are just examples. You can replace the cat with any program reading from the STDIN.
  • tripleee
    tripleee almost 3 years
    No, this is not POSIX; process substitutions are Bash only, just like here strings.
  • tripleee
    tripleee almost 3 years
    Actually, type ctrl-D to finish. Ctrl-C will kill the script too, which you probably don't want.
  • jspencer
    jspencer over 2 years
    Any idea how the performance of this compares to pipes?