continuously print string as if being entered from the keyboard

cycle(){ while :;do((i++));echo -n "${3:$(($i%${#3})):1}";sleep .$(($RANDOM%$2+$1));done;}
Cycles continuously through a string printing each character with a random delay less than 1 second. First parameter is min, 2nd is max. Example: 1 3 means sleep random .1 to .3. Experiment with different values. The 3rd parameter is the string. The sleep will help with battery life/power consumption. cycle 1 3 $(openssl rand 100 | xxd -p) Fans of "The Shining" might get a kick out of this: cycle 1 4 ' All work and no play makes Jack a dull boy.'

2010-10-08 23:45:40

9 Alternatives + Submit Alt

What Others Think

Hello ! Please, can somebody explain to me how does this expression "${3:$(($i%${#3})):1}" work ?
JisSey · 564 weeks and 4 days ago
ok I got it now ;) {array:position:number of element to get} array=3 is a substitution for $3 (the 3rd parameter) when using the function call i%array_length is a loop cylcling between the vallues 0,1,2,3...,(array_length - 1) I have improved a bit the function by initializing the iterative variable so the printing can start from the first letter of the sentence => cycle(){i=$((${#3}-1));while :;do((i++));echo -n "${3:$(($i%${#3})):1}";sleep .$(($RANDOM%$2+$1));done;}
JisSey · 564 weeks and 4 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: