1. Overview

Shell scripting helps when performing different actions automatically. In particular, we can automate the installation of applications on a server or for deploying applications to numerous systems in parallel.

In this tutorial, we’ll see how to write a script to install multiple applications in Linux. Specifically, we’ll work with Bash to create our scripts.

Also, we created this tutorial on Ubuntu 22.04. However, the commands used in the scripts should work on most other Linux systems with little or no modification.

2. Bash Scripting and Installations

In general, scripts are a very helpful tool for system administrators and developers. Scripting removes most of the need for time-consuming manual operations. Further, scripting enables us to get the desired output in a single and efficient step. Specifically, there are several advantages of using Bash scripts:

  • Bash offers a flexible syntax as well as many other significant features
  • Bash scripts can be used in a POSIX-compliant mode
  • Bash serves as the default login shell for many Linux distributions
  • Bash can run all sh commands since it builds on top of sh

Of course, Bash scripting enables users to automate tasks. Hence, it’s usually a good choice for managing system administration tasks.

In Linux, we usually leverage package managers like apt or yum to install applications. Notably, performing installations in parallel simplifies the process in general. As usual, automation also reduces the risk of human errors.

For example, scripts can detect the current package manager a system uses and store its installation command in a variable:

  • installcom=’apt install’
  • installcom=’yum install’
  • installcom=’dnf install’

After that, we can use $installcom instead of the relevant command.

Let’s see a few ways to install multiple applications in Linux using Bash scripts.

3. Using Arrays

One way to install multiple applications using a script is to use an array. Basically, an array is a variable that can store several elements.

First, we’ll create an array to hold the names of the applications we want to install:

# Create an array of applications to install
applications = ["app1", "app2", "app3"]

Then, we’ll iterate over the array and call the installer for each application:

# Install applications
sudo apt install -y "${apps[@]}"

Let’s demonstrate by installing several applications:

We can now write a script using the cat command, say array_demo.sh, to install the above applications:

$ cat > array_demo.sh
#!/bin/bash
apps = ["dconf-cli", "elinks", "wget"]
sudo apt update -y
sudo apt install -y "${apps[@]}"
^C

Let’s make the script executable with the chmod command:

$ chmod +x array_demo.sh

Next, we run the script using the indirect call method:

$./array_demo.sh
Hit:4 http://in.archive.ubuntu.com/ubuntu...
Reading package lists... Done
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages ...
  elinks-data libev4 libfsplib0 ...
...

Overall, using arrays can be a good choice for small-scale installations. However, it may become less practical as the number of applications grows.

4. Using Command-Line Arguments

We can also specify the applications to install as command-line arguments. The script can then parse these arguments and install the selected applications accordingly.

Let’s see a basic example by creating a new script file, cli_arg_demo.sh:

$ cat cli_arg_demo.sh
#!/bin/bash
sudo apt update -y
for app in "$@"; do
  echo "Installing $app..."
  sudo apt install -y "$app"
done

In the above script, the $@ syntax represents all the command-line arguments as an array. The for loop then iterates over these arguments.

Notably, the $app variable contains the name of the current application from the command-line arguments. The echo statement prints a message indicating which application is being installed.

Finally, the last statement of the for loop installs the applications specified on the command line.

Let’s again make the above script executable:

$ chmod +x cli_arg_demo.sh

Then, we pass the name of the applications to be installed as command-line arguments:

$ ./cli_arg_demo.sh dconf-cli wget elinks
Hit:4 http://in.archive.ubuntu.com/ubuntu...
Reading package lists... 
Building dependency tree       
Reading state information... 
Installing dconf-cli...
The following NEW packages will be installed:
  dconf-cli
0 upgraded, 1 newly installed...
...
Installing wget
...

Generally, command-line arguments can add complexity to the script for a large number of applications. Potentially, this makes the script less readable.

5. Using a Plain Text File

Another way to install a number of applications using a script is to use a plain text file.

To that end, let’s first create a file, app_list.txt for example, that contains the names of the applications we want to install:

$ cat app_list.txt
wget
elinks
dconf-cli

Each entry should reside on a separate line. Next, we create a script, install_from_file.sh:

$ cat > install_from_file.sh
#!/bin/bash
app_file=$1
sudo apt update -y
while IFS= read -r app
do
  echo "installing "$app""
  sudo apt -qq install "$app" -y
done < "$app_file"

Let’s try to understand the main elements of this script:

  • app_file contains the path to the plain text file as passed via the first command-line argument
  • the while loop reads lines from the file specified by $app_file using the read command
  • IFS= prevents leading and trailing whitespace from being trimmed from each line
  • the -r option of read preserves backslashes in the lines
  • the > redirection operator at the end of the loop supplies the input file contents

Here, the above script takes a plain text file as input via the command line argument $1.  It then updates the software repository. Each line of the file should contain the name of an application to be installed.

Furthermore, the while loop uses the IFS= read -r app command to read a line from the app_file file into the app variable. The while loop then installs the applications until there are no more lines to read from the app_file file.

Also, it displays messages for each installation step.

Let’s make the above script executable:

$ chmod +x install_from_file.sh

Then, we run the script with the name of the text file as an argument. This installs the packages listed in the text file:

$ ./install_from_file.sh app_list.txt

Particularly, this method keeps a consistent list of application installations over multiple systems. However, it requires us to create and maintain a plain text file.

6. Conclusion

In this article, we saw various ways to write a script for installing multiple applications.

In conclusion, each approach has its strengths and limitations. The choice of method depends on our specific use case and preferences.

Comments are closed on this article!