I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the "pause" command. Is there a Linux equivalent I can use in my script?
Join Stack Overflow to learn, share knowledge, and build your career.
|
The If you are using Bash, you can also specify a timeout with
|
|||||||||
|
|
I use these ways a lot that are very short, and they are like @theunamedguy and @Jim solutions, but with timeout and silent mode in addition. I especially love the last case and use it in a lot of scripts that run in a loop until the user presses Enter. Commands
Explanation-r specifies raw mode, which don't allow combined characters like "\" or "^". -s specifies silent mode, and because we don't need keyboard output. -p $'prompt' specifies the prompt, which need to be between $' and ' to let spaces and escaped characters. Be careful, you must put between single quotes with dollars symbol to benefit escaped characters, otherwise you can use simple quotes. -d $'\e' specifies escappe as delimiter charater, so as a final character for current entry, this is possible to put any character but be careful to put a character that the user can type. -n 1 specifies that it only needs a single character. -e specifies readline mode. -i $'Y' specifies Y as initial text in readline mode. -t 5 specifies a timeout of 5 seconds key serve in case you need to know the input, in -n1 case, the key that has been pressed. $? serve to know the exit code of the last program, for read, 142 in case of timeout, 0 correct input. Put $? in a variable as soon as possible if you need to test it after somes commands, because all commands would rewrite $? |
|||||||||||||
|
|
|
|||
|
|
|
Besides using
|
|||||||||||||||||||||
|
|
This worked for me on multiple flavors of Linux, where some of these other solutions did not (including the most popular ones here). I think it's more readable too...
Note that a variable needs to be supplied as an argument to |
|||||||||
|
|
If you just need to pause a loop or script, and you're happy to press Enter instead of any key, then |
|||
|
|
|
Try this:
|
||||