How can I automate a bash script that is 'read'-ing from /dev/tty?

I have a script (that I cannot modify) that I must run regularly that has the following construct:

read -r response < /dev/tty
if [ "$response" = 'y' ]; then

I want to wrap this script such that I always send a "y" character to it. I've tried running the script with the standard tricks for sending a yes:

echo y | '/bin/bash /path/to/script'


yes | ./myscript


printf "y\n" | ./myscript


/bin/bash /path/to/script < /path/to/y-file

None of these work. I also tried expect.

It's not clear if Mac OS X' built-in expect is working; with expect diagnostic information enabled, expect is properly detecting to the prompt and responding, but the script then terminates.

set timeout -1

expect -d <<EOF
spawn $SCRIPT
expect "prompt string"
send "y\r"

Also, to clarify, this is on Mac OS X Monterey.

I'd appreciate any help. Again, I cannot modify the original script.

2 answers

  • answered 2022-01-25 17:37 ceving

    You can use socat to fake a new pseudo terminal for a child process.

    Let be the following script:

    #! /bin/bash
    read -r response < /dev/tty
    if [ "$response" = 'y' ]; then
      echo yes
      echo no

    Then you can connect stdin to the new pty of the child process this way:

    echo y | socat stdio exec:./,pty,setsid,echo=0

  • answered 2022-01-25 18:38 phranz

    You could try to use autoexpect to generate the wrapper (autoscript) script for you:

    autoexpect -f autoscript ./

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum