What is the Linux Equivalent to DOS Pause?

LinuxOperating SystemOpen Source

We know that the Pause command in DOS is used to suspend execution of the batch files and it then displays the message

Strike a key when ready ...

It should also be noted that some versions of DOS also allow a comment to be entered on the same line as PAUSE.

Example

We can make use of the Pause command in a scenario where we want to suspend the execution of a batch file and display the message “Insert Code”, by typing the following command in the terminal

pause Insert Code

So, that was all about the Pause command in DOS, but we want to know how we can achieve the same thing in Linux, since Linux by default doesn’t provide the pause command utility.

There are different approaches we can go for in order to achieve the exact same behavior as of the Pause command, the first and the most common approach is to make use of the read command.

Let’s first learn a few things about the read command.

Read command in the Linux system is used to read from a file descriptor. The read command then splits the line into words.

Syntax

read [options] [name ...]

We can replace the options placeholder in the syntax shown below with the following options mentioned in the table below

-a array assign the words read to sequential indices of the array variable ARRAY, starting at zero
-d delim continue until the first character of DELIM is read, rather than newline
-e use Readline to obtain the line
-i text use TEXT as the initial text for Readline
-n nchars return after reading NCHARS characters rather than waiting for a newline, but honor a delimiter if fewer than

The command that is equivalent to DOS Pause is mentioned below

read -n1 -r -p "Press any key to continue..." key

In the above command there are certain flags that are used. These are −

  • -n1 − It is used to specify that it only waits for a single character.

  • -r − Used to put it into raw mode, which is necessary as if we somehow press something like backlash, it doesn’t register until you hit the next key.

  • -p − it specified the prompt.

  • key − the key argument is only necessary if you want to know which key they pressed

raja
Published on 31-Jul-2021 12:22:52
Advertisements