Finding a BASH alternative to the timeout utility

Coming from a mostly Windows background, making the switch from Windows Batch to BASH is not always the easiest to get used to at first. I never depended on Batch scripts in Windows because most everything I needed was available through the GUI, and there was not much to automate on my computer at the time anyways. But whenever I did, I made extensive use of the timeout utility so that the console window would show me information without closing it right away. If there was a problem with an operation, it would make it harder for me to find out if the window simply flashed the information at me.

There is technically a timeout utility bundled with Linux, but it works much differently than the Windows version as it expects to run a given command, and then times out of it after the given time (e.g. 5 seconds). It has it’s uses in a scripting environment, and I might find uses for it later on, but for the purpose of pausing the script in order for the user to read something before continuing, it is useless for me.

After some digging, I found that the best way to do this is the read command. Since the goal is to get input from the user, this seems to be the best command for the job. With certain flags set, it can also work in pretty much the same way as the Windows version does. The only downside at the moment is that the timeout option doesn’t count down how many seconds it’s going to wait in real time (e.g. Closing in ‘x’ seconds…). I might find a way to do so later on, but to keep this article simple, it will be a topic for another time.

read -n1 -p “Press any key to continue…”
echo

To better understand what’s going on, I’m going to break down each flag used, and why they are being used here. The -n1 flag lets the read command know to stop user input after one key is pressed. Since we’re not going to use the input for anything, it doesn’t matter what we press, so long as we press something. This also has the side effect of showing the character that we’ve input, but it should not affect functionality. Next, the -p flag shows the text to the user so they know what to do, and not assume that the script is frozen. I’m sure you could technically use either the echo or printf command here before using the read command, but I find it to be a little bit easier to read later on.

Finally, the echo command, while not necessary for operation, helps with formatting as leaving it out will make everything appear on the same exact line, making it harder to read:


 Press any key to continue...user@machine:~
$  

Hopefully, this will help you when writing BASH scripts in the future. I hope it helps you out.

DISCLAIMER: While I make a lot of effort to make sure the content I provide is high quality, that doesn’t mean that it will always work. If you find a problem in any of my posts, please let me know right away, and I will fix it to the best of my ability. Also, please be nice. We’re all mature individuals here, and we’re all still learning, even if we’re not currently in the classroom.

Comments