1

How can I have a delay between words to simulate an actual human typing all in one horizontal line? For loops only display vertically as far as I know.

This pseudocode is what I have in mind:

    echo "hello" && sleep 1 && echo "world" && sleep 1 && echo "!"

Something like that.

5
  • What do you mean "for loops only display vertically"? You can write a loop as for i in a b c; do echo "$i"; done, but I don't see how that addresses the question at all. I guess you could do for word in hello world \!; do echo "$word" && sleep 1; done Commented May 5, 2022 at 19:53
  • 2
    Add -n to each echo but the last. Commented May 5, 2022 at 19:55
  • @pmf thnx, that did the trick Commented May 5, 2022 at 20:02
  • expect can actually simulate typing, with delays between each character. For fun, run 99-bottles-of-beer.net/language-expect-249.html Commented May 5, 2022 at 20:05
  • echo -n "hello" && sleep 1 && echo -n " world" && sleep 1 && echo "!"? Commented May 5, 2022 at 20:21

1 Answer 1

2

This is actually quite a cool tool called pv standing for pipe viewer, but can simulate what you're looking for, I found the answer here.

TL;DR:

echo "You can simulate on-screen typing just like in the movies" | pv -qL 10

A little longer answer:

pv allows you to monitor data sent through a pipe, hence pipe viewer. This is done visually and therefore it has visual monitoring capabilities. In your case we use

L - this limits the character transfer rate, bytes-per-second q - quiet, otherwise you will a breakdown analysis of how long each package took to go through the pipe.

Sign up to request clarification or add additional context in comments.

5 Comments

thnx, i tried it and really liked it. how would you add that to a shell script without the giving you the error 'Syntax error: "(" unexpected'?
When I ran it in a script it worked. Maybe edit the original question with an example script so we can try and debug it?
Do you have a shebang '/bin/bash' of some sort at the beginning of your script?
correct. line #!/bin/bash was there. could you please post your 2-liner? the shebang + the pv command? so close yet so far away lol this is what i have #!/bin/bash echo "hello world" | pv -qL $[10+(-2 + RANDOM%5)]
i think i figured it out, i needed to run from the shell: /bin/bash test.sh i was using just sh test.sh --behavior i have never seen before. usually sh does it for me. thank you for all your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.