You're asking the wrong question, and you already know the answer to the question you're asking.
For the question "How to actually echo text to a command after a delay, using bash?", the answer is precisely:
{ sleep $DELAY; echo $TEXT; } | command
However, that should hardly ever be necessary. It provides the given text to command
's standard input after the given delay, which may cause the command to wait a bit before proceeding with the read input. But there is (almost) never a case where the data needs to be delayed until the command is already waiting for it -- if the command is reading from standard input.
In the case of mtools
, however, mcopy
is not reading the clash code from standard input. Instead, it is reading it directly from /dev/tty
, which is the terminal associated with the command. Redirecting standard input, which is what the bash pipe operator does, has no effect on /dev/tty
. Consequently, the problem is not that you need to delay sending data to mcopy
's standard input; the problem is that mcopy doesn't use standard input, and bash has no mechanism to hijack /dev/tty
in order to fake user input.
So the other question might be "how to programmatically tell mcopy
which clash option to use?", but apparently you know the answer to that one, too: use the -D
command line option (which works with all relevant mtools
utilities).
Finally, a more complicated question: "Is there some way to automate a utility which insists on reading input from /dev/tty
?" Here, the answer is "yes" but the techniques are not so simple as just piping. The most common way is to use the expect
utility, which allows you to spawn a subprocess whose /dev/tty
is a pseudo-tty which expect
can communicate with.