Chris
Chris

Reputation: 484

How to make APT assume yes and force yes for all installations in a Bash script

I'm currently getting into Linux and want to write a Bash script which sets up a new machine just the way I want it to be.

In order to do that I want to install different things on it, etc.

What I'm trying to achieve here is to have a setting at the top of the Bash script which will make APT accept all [y/n] questions asked during the execution of the script.

A question example I want to automatically accept:

After this operation, 1092 kB of additional disk space will be used. Do you want to continue? [Y/n]

I just started creating the file, so here is what I have so far:

#!/bin/bash

# Constants

# Set APT to accept all [y/n] questions
>> some setting here <<

# Update and upgrade APT
apt update;
apt full-upgrade;

# Install terminator
apt install terminator

Upvotes: 16

Views: 22073

Answers (6)

user5127
user5127

Reputation: 159

There is no need to use apt-get. You can use the new and improved apt by using:

sudo apt install -y package1 package2 ... packageN

Where package placeholders are the packages you want to install.

P.S. I'm not sure if three years ago apt did not support the -y option.

Upvotes: 0

TUX
TUX

Reputation: 117

Another easy way to set it at the top of your script is to use this command:

alias apt-get="apt-get --assume-yes"

This will cause all subsequent invocations of apt-get to include the --assume-yes argument. For example, apt-get upgrade would automatically get converted to apt-get --assume-yes upgrade by Bash.

Please note that this may cause errors, because some apt-get subcommands do not accept the --assume-yes argument. For example, apt-get help would be converted to apt-get --assume-yes help which returns an error, because the help subcommand can't be used together with --assume-yes.

Upvotes: 0

logbasex
logbasex

Reputation: 2242

You can set up APT to assume 'yes' permanently as follows:

echo "APT::Get::Assume-Yes \"true\";\nAPT::Get::allow \"true\";" | sudo tee -a  /etc/apt/apt.conf.d/90_no_prompt

Upvotes: 1

Cyrus
Cyrus

Reputation: 88553

With apt:

apt -o Apt::Get::Assume-Yes=true install <package>

See: man apt and man apt.conf

Upvotes: 5

Christian Fritz
Christian Fritz

Reputation: 21354

If you indeed want to set it up once at the top of the file as you say and then forget about it, you can use the APT_CONFIG environment variable. See apt.conf.

echo "APT::Get::Assume-Yes=yes" > /tmp/_tmp_apt.conf
export APT_CONFIG=/tmp/_tmp_apt.conf

apt-get update
apt-get install terminator
...

Upvotes: 3

mivk
mivk

Reputation: 14824

apt is meant to be used interactively. If you want to automate things, look at apt-get, and in particular its -y option:

-y, --yes, --assume-yes

Automatic yes to prompts; assume "yes" as answer to all prompts and run non-interactively. If an undesirable situation, such as changing a held package, trying to install an unauthenticated package or removing an essential package occurs then apt-get will abort. Configuration Item: APT::Get::Assume-Yes.

See also man apt-get for many more options.

Upvotes: 19

Related Questions