Search code examples
bashstdouttty

Redirect to a script that uses /dev/tty


I am writing a git hook that might require human input. According to this answer one has to use exec < /dev/tty in that script. This does the job, but now there is no possibility to redirect the standard output to that hook (for test purposes). I guess the problem can be narrowed down to a question: how to send a message to /dev/tty in such a way that another process will read it? Not sure if this is even possible.

Here is the minimum reproducible example:

# file: target.sh

exec < /dev/tty # we want to use /dev/tty
read -p  "Type a message: " message
echo "The message ${message}"

I tried several solutions like this:

echo -e "foo\n"| tee /dev/tty | source target.sh

And it actually prints the message in the console after the read prompt, but the message variable remains unset. Is there any way to fix it?


Solution

  • You can use expect to achieve the result:

    #!/bin/bash
    
    expect << EOF
    spawn bash target.sh
    expect {
        "Type a message: " {send "foo\r"; interact}
    }
    EOF