Celeb Glow
general | March 27, 2026

How do I write a shell script to install a list of applications?

Does anyone know how to write a shell script to install a list of applications? It's a pain to have to install each application by hand every time I set up a new system.

Edit:It still asks me Do you want to continue [Y/n]?. Is there a way to have the script input y or for it not to prompt for input?

1

5 Answers

I would assume the script would look something like this:

#!/bin/sh
apt-get update # To get the latest package lists
apt-get install <package name> -y
#etc.

Just save that as something like install_my_apps.sh, change the file's properties to make it executable, and run it from the command line as root.

(Edit: The -y tells apt-get not to prompt you and just get on with installing)

4

Well, according to your question the easiest script would be:

#!/bin/sh
LIST_OF_APPS="a b c d e"
aptitude update
aptitude install -y $LIST_OF_APPS

However you could also enter aptitude update && aptitude install -y a b c d e. So maybe your question is missing the crucial point here. If there are some further requirements it would be nice to explain them.

1

Just create a list of apps in a file, example.list, and run

cat example.list | xargs sudo apt-get -y install
2
#!/bin/bash
set -eu -o pipefail # fail on error and report it, debug all lines
sudo -n true
test $? -eq 0 || exit 1 "you should have sudo privilege to run this script"
echo installing the must-have pre-requisites
while read -r p ; do sudo apt-get install -y $p ; done < <(cat << "EOF" perl zip unzip exuberant-ctags mutt libxml-atom-perl postgresql-9.6 libdbd-pgsql curl wget libwww-curl-perl
EOF
)
echo installing the nice-to-have pre-requisites
echo you have 5 seconds to proceed ...
echo or
echo hit Ctrl+C to quit
echo -e "\n"
sleep 6
sudo apt-get install -y tig

Explanation:

  • set -eu -o pipefail command:

    Command elementsExplanation
    setModify how the shell environment operates
    -uIf a variable does not exist, report the error and stop (e.g., unbound variable)
    -eTerminate whenever an error occurs (e.g., command not found)
    -o pipefailIf a sub-command fails, the entire pipeline command fails, terminating the script (e.g., command not found)

    If this script encounters any errors, it will fail and exit.

    Ref:

  • sudo -n true command:

    Command elementsExplanation
    sudoRun as superuser
    -nNon-interactive. Prevents sudo from prompting for a password. If one is required, sudo displays an error message and exits
    trueBuiltin command that returns a successful (zero) exit status

    Run as a superuser and do not ask for a password. Exit status as successful.

    Ref: ,

  • test $? -eq 0 || exit 1 "you should have sudo privilege to run this script" command:

    Command elementsExplanation
    testTakes an expression as an argument, evaluates it as '0' (true) or '1' (false), and returns the result to the bash variable $?
    $?A variable used to find the return value as the exit status of the last executed command
    -eqequals
    0Value result is true
    ||Logical "OR" is a Boolean operator. It can execute commands or shell functions based on the exit status of another command
    exitExits the shell with a status of N. If N is unspecified, it uses the exit code of the last executed command
    1Value result is false and used here as an argument to the exit command to use as an exit code
    "you should have sudo privilege to run this script"If the exit code is false, print this message to the terminal

    Test the last variable's exit code and see if it equals '0'. If not, exit with an error and print a given message to the terminal.

    Ref: , , ,

  • echo installing the must-have pre-requisites command:

    Command elementsExplanation
    echoBuiltin command used to print information or messages to the terminal
    installing the must-have pre-requisitesMessage to print to the terminal

    Tell the user that it is going to install some pre-requisite packages before installing the actual program.

    Ref:

  • while read -r p ; command:

    Command elementsExplanation
    whileCreate a while-loop, i.e. perform a given set of commands ad infinitum as long as the given condition evaluates to true
    readRead a line from the standard input and store it in a variable
    -rOption passed to read command that avoids the backslash escapes from being interpreted
    pArbitrary variable for read to store captured input. Here it represents each package to be installed
    ;Control operator AND. Proceed to the next command and execute it regardless of the exit status of the previous command (execute even if the previous command fails)

    Read a given file line by line forever or until receiving a value of "false," then continue onto the proceeding command.

    Ref: , , ,

  • do sudo apt-get install -y $p ; command:

    Command elementsExplanation
    doReserved word used to delimit the sequence of commands which follow. i.e., start
    apt-getTool used by the Debian APT (Advanced Package Tool) package manager
    installA command used to install packages
    -yLong-form is --yes. assume "yes" on all query prompts
    $pUsed to call the arbitrary variable p from read and use it as standard input

    Install the list of packages as a superuser without prompting for confirmation to install.

    Ref:

  • done < <(cat << "EOF" <list of packages> EOF) command:

    Command elementsExplanation
    doneReserved word used to delimit the sequence of commands which precede. i.e., stop
    <Redirection to the standard input
    catConcatenate. used for viewing, creating, and appending files
    <<Redirection that reads input from the current source until encountering a delimiter and then using those lines as the standard input for a command
    EOFEnd of File delimiter
    cat << EOF-EOFThis will read, then print everything enclosed within the EOF block
    <(list)Obtain the output of the list; parentheses indicate that the list will execute in a subshell environment

    Read the list of packages and gather them as standard input. Redirect it to the read command, which captures it as the p variable, and then sends it to the $p variable, which allows it to get executed by the install command, and when it reaches the EOF delimiter, redirect the output to done effectively ending the while read loop.

    Ref: ,

The following four echo messages are self-explanatory:

  • echo installing the nice-to-have pre-requisites
  • echo you have 5 seconds to proceed ...
  • echo or
  • echo hit Ctrl+C to quit

however, the next one is not.

  • echo -e "\n" command:

    Command elementsExplanation
    -eEnable the function of backslash characters
    \nBackslash escaped sequence for new line

    This command creates a newline.

  • sleep 6 command:

    Command elementsExplanation
    sleepDelay the execution of a bash script, typically for N seconds, unless using an option to indicate longer lengths of time

    Delay the execution of the following command for 6 seconds.

    Ref:

  • sudo apt-get install -y tig command:

    Install the tig package with the Debian apt-get tool while running the installation as a superuser, and do not prompt for confirmation.

General references:

1

I would opt for the following script: vim install

#!/bin/bash
apt-get update # To get the latest package lists
apt-get install $1 -y

Then I should make the above script executable chmod +x install. Then to use it, I could type: ./install <package_name>. Example: ./install clang

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy