Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm a total noob at bash scripting and right now I'm just reading various tutorials trying to get a feel for it. As an exercise I'm trying to write up a script for my kids that asks a series of questions and waits for user input before proceeding to the next question.
I've got that part down, but I was thinking that it would be cool if I could get the script to "type out" the text in the questions rather than just blip out the whole question in one shot. In other words, the questions would appear as if someone were typing them live.
I hope this is clear.
For example, rather than blip out the question "what is your name?" I'm trying to get it to appear as if someone were typing it out so it would appear like "w (pause) h (pause) a (pause) t (pause)" and so on.
Originally posted by Matir Well, thought it would be slow, you could do:
Code:
echo -n H
sleep 1
echo -n e
sleep 1
echo -n l
sleep 1
echo -n l
sleep 1
echo -n o
Not very easy, either.
Hi Matir,
Thanks for that. That is exactly what I am looking for, but like you said it wouldn't be easy. Is there an easier way? Could I type out a text file then write a script to cat that text file and write it all up like you suggested?
Could I make the typing any faster?
Thanks for your help!
LocoMojo
Edit: I found a way to make it type faster. "sleep .2"
Unfortunately, bash's sleep is only in whole-second increments. If you'd like, I could write you a program that would print strings at a somewhat faster speed. It would be trivial, and you wouldn't have to work with C code, I promise.
Originally posted by Matir Unfortunately, bash's sleep is only in whole-second increments. If you'd like, I could write you a program that would print strings at a somewhat faster speed. It would be trivial, and you wouldn't have to work with C code, I promise.
Hi Matir,
Apparently my edit of my last post and your last post crossed.
I found a way to make it type faster.
sleep .2
So I guess it isn't always in whole second increments
I appreciate your offer in writing a program for me, but that would defeat the purpose of my learning to script. C coding is way, way, way out of my league.
Ah, you are correct. GNU sleep does support such an extension.
You could try something like:
Code:
function typestring {
STRING="${*}" # extract all arguments
for i in `seq 0 $((${#STRING}-1))` #offset 0, length-1
do echo -n "${STRING:$i:1}" #extract one char
sleep .2
done
echo
}
typestring "hello world!"
Originally posted by Matir Ah, you are correct. GNU sleep does support such an extension.
You could try something like:
Code:
function typestring {
STRING="${*}" # extract all arguments
for i in `seq 0 $((${#STRING}-1))` #offset 0, length-1
do echo -n "${STRING:$i:1}" #extract one char
sleep .2
done
echo
}
typestring "hello world!"
Wow, that looks awful complicated, but it works well except it doesn't put a space between hello and world.
Thanks a bunch for this. Now I'll have to learn how it works
it worked for me with a space between hello and world. Make sure all the quotes are preserved, ESPECIALLY on the echo -n line.
I'll try to break the script down here for general education:
Code:
function typestring {
This is bash's declaration of a function.
Code:
STRING="${*}"
We place all arguments to the function in the variable $STRING
Code:
for i in `seq 0 $((${#STRING}-1))`
This is a complex line. It sets up a for loop with values from the sequence 0 .. string length -1. ${#VARIABLE} is the length of a string. $(()) does math evaluation. seq generates a sequence. Bacticks (``) perform command substitution.
Code:
do echo -n "${STRING:$i:1}"
The -n to echo prevents echo from adding a newline. ${STRING:$i:1} tells bash to extract the $i'th character from the string $STRING. The quotes allow us to echo characters like spaces.
Code:
sleep .2
Sleeps for 2/10ths of a second.
Code:
done
End the loop.
Code:
echo
This blank echo adds a newline to the end of the task.
Code:
}
This ends the definition of the typestring function.
Originally posted by Matir it worked for me with a space between hello and world. Make sure all the quotes are preserved, ESPECIALLY on the echo -n line.
I'll try to break the script down here for general education:
Code:
function typestring {
This is bash's declaration of a function.
Code:
STRING="${*}"
We place all arguments to the function in the variable $STRING
Code:
for i in `seq 0 $((${#STRING}-1))`
This is a complex line. It sets up a for loop with values from the sequence 0 .. string length -1. ${#VARIABLE} is the length of a string. $(()) does math evaluation. seq generates a sequence. Bacticks (``) perform command substitution.
Code:
do echo -n "${STRING:$i:1}"
The -n to echo prevents echo from adding a newline. ${STRING:$i:1} tells bash to extract the $i'th character from the string $STRING. The quotes allow us to echo characters like spaces.
Code:
sleep .2
Sleeps for 2/10ths of a second.
Code:
done
End the loop.
Code:
echo
This blank echo adds a newline to the end of the task.
Code:
}
This ends the definition of the typestring function.
Code:
typestring "hello world!"
This calls typestring to print out hello world!
I hope that helps some.
Hi Matir,
Funnily enough, when I copied the code from your post the quotes from:
do echo -n "${STRING:$i:1}" #extract one char
didn't come with it...strange. I fixed it and it works like a charm.
I appreciate your breaking it down like you did and explaining each bit. It makes more sense to me now. Thanks much for that!
I have one last question and then I won't bother you anymore, I promise
$(( EXPRESSION )) is math evaluation.
$( EXPRESSION ) is command substitution (think ` `)
${ VARIABLE } is a variable reference. For just a variable, it's equivalent to $VARIABLE.
[ ] is used to access an array... I suppose. Honestly, I've never used arrays in bash.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.