In this tutorial, we’ll look at the methods we can use to bypass the confirmation prompts triggered by shell scripts and programs. We’ll make use of the existing commands in Bash to automate the script execution. Additionally, we will write a small Bash script that we’ll use throughout the tutorial.
2. Using the echo Command
The simple echo command outputs strings passed to it as arguments. It’s the holy grail of shell scripts when it comes to writing verbose scripts because it’s ubiquitous. And not only that, but it’s also very facile to use.
As an example, let’s print a piece of text to standard output:
$ echo "I am being echoed" I am being echoed
By default, it will print an implicit newline character if no arguments are provided:
We can use that to our advantage to simulate an enter keypress:
$ echo | <command>
Suppose we want to install a package using Pacman, and we need to skip the confirmation message. We can simply pipe the implicit newline character from echo to the pacman command:
# echo | pacman -S firefox
Alternatively, we can be more explicit with the echo command. The -ne options in the command let echo know that we’re using newline and escape characters in our text:
# echo -ne '\n' | pacman -S firefox
2.1. Using echo to Bypass Prompts
Let’s create a scenario where we have a text file that contains a list of packages that we need to install. We’ll pass this batch file to a script as an argument, which in turn will install the packages.
Let’s create a text file called pkgs.txt and put the package names inside it:
mpv ffmpeg dunst ranger feh
Next, we’ll write a script that will take the text file as an argument and carry out some validations. Afterward, it will loop through the contents of the text file and install the packages from the official repository using the pacman package manager:
#!/bin/bash # Check whether the file is provided if [[ $# -eq 0 ]]; then echo 'Usage: ./install_packages.sh <file>' exit 0 fi # Check whether the file exists if [[ ! -f "$1" ]]; then echo "$1: No such file" exit 1 fi pkgs="" # Loop through the text file line by line while IFS= read -r pkg; do # Append the package name to pkgs pkgs="$pkgs $pkg" done < "$1" # Install the packages pacman -S $pkgs
Now, we need to save the file and execute it. Mind that we’ll need root access to execute the script:
# ./install_packages.sh pkgs.txt resolving dependencies... looking for conflicting packages... Packages (5) dunst-1.6.1-2 feh-3.7-1 ffmpeg-2:4.4-4 mpv-1:0.33.1-2 ranger-1.9.3-3 Total Installed Size: 41.39 MiB Net Upgrade Size: 0.00 MiB :: Proceed with installation? [Y/n]
As we can see, it prompts a confirmation to install the packages. We’ll simply make use of the echo command to bypass it:
# echo | ./install_packages.sh pkgs.txt
Now, as long as there are no errors, the script will execute without the annoying confirmation prompt.
3. The yes Command
The yes command outputs a string to the standard output repeatedly until terminated. It ships with the coreutils package, which is pre-installed on most Linux distributions. Despite its simple functionality, it’s a very useful utility for different scenarios. Apart from creating a CPU spike, we can also use it to automate a script.
Let’s try it in the terminal without any arguments:
$ yes y y y y y^C
We can kill the process by pressing <CTRL + C>. It’s useful in scenarios if we need to bypass multiple confirmation prompts in shell scripts. Let’s change our script a little bit so that it installs the packages one by one, as opposed to installing them as a whole:
#!/bin/bash # Check whether the file is provided if [[ $# -eq 0 ]]; then echo 'Usage: ./install_packages.sh <file>' exit 0 fi # Check whether the file exists if [[ ! -f "$1" ]]; then echo "$1: No such file" exit 1 fi # Loop through the file line by line and install the packages for pkg in $(cat $1); do pacman -S $pkg done
The script will now install the packages one by one synchronously. However, each time the pacman command is executed, we’ll get a confirmation prompt. The echo command will only work for the first prompt, but the yes command will respond to more than one prompt. Therefore, we’ll make use of the yes command:
# yes | ./install_packages.sh pkgs.txt
The script will now run smoothly for every pacman command in the loop without stopping at confirmation prompts.
4. The expect Utility
The expect utility is a program designed to automate the execution of other programs. As the name suggests, expect knows what can be expected from another program and what to respond to that program.
The expect program can be installed from the official package repositories using a package manager. It’s available under the expect package name. Once installed, confirm it with the following command:
$ expect -v expect version 5.45.4
To use the expect program, we create an expect script that contains all the behavior needed to interact with a program. The expect commands provides several actions:
- spawn – for starting a script
- expect – to wait for a program output
- send – for replying to a program
- interact – for manually interacting with a program
However, writing a long expect script wastes a lot of time. Fortunately, expect comes with the autoexpect utility, which we’ll use to generate the script automatically for us:
# autoexpect ./install_packages.sh pkgs.txt
Once it completes, it will write its output to the script.exp file. So, next time, when we need to run our install_packages.sh script, we will spawn it with the script.exp file:
Once we execute the expect script, we can observe that it responds to our installation script automatically using the script.exp template.
5. Workaround with Here Document
In Bash, a heredoc or here document is a section that is used as an input to a command. This section is treated as a separate file or an input stream by a command that comes before it. As an example, if we want to provide the cat command a stream of text in a Bash script, we can make use of the here document:
#!/bin/bash cat <<END Hello, World! END
Let’s break the example down:
- The cat command prints a stream of text to standard output from a file or standard input.
- The <<END keyword indicates the beginning of a here document section.
- The contents inside this section are fed to the cat command. In our case, the cat command will print whatever is inside the here document.
- The final END keyword indicates the end of the here document section.
Let’s save the script to a file and execute it:
$ ./heredoc.sh Hello, World!
As we can see, it prints the text inside the here document section. For our purpose, we can use the here document below to bypass the confirmation prompts. We’ll simply supply two blank lines in the here document section after a command that prompts us for confirmation.
In our package installation script, we’ll simply add the here document section with blank lines:
for pkg in $(cat $1); do pacman -S $pkg <<END END done
Once we execute the script, the heredoc will act as if we are pressing the Enter key twice. As a consequence, the prompts will be confirmed automatically.
6. Programs with an Auto-Confirmation Flag
While we can simulate the Enter key in our custom Bash scripts, sometimes there are programs that provide the option to bypass the confirmation prompts. For instance, the pacman command avails the –noconfirm flag:
$ sudo pacman -Sy --noconfirm doge
The same is true for apt and yum package managers. For apt, there is an option –assume-yes:
$ sudo apt install --assume-yes speedcrunch
Similarly, there are other programs that also avail the option to execute quietly without prompts. Therefore, it’s a good practice to check for this option before trying out the methods in the aforementioned sections.
In this tutorial, we went through various methods of simulating the Enter key in Bash. We used the echo and yes commands to serve that purpose. We also got familiar with the expect utility and the heredoc feature of Bash to automate the execution process.