非开发人员求助:如何在Bash脚本中用列表执行apt-get install
Got it, let's make this straightforward and easy to follow—no fancy programming hoops to jump through. Here's exactly how to organize your software into a reusable list and call it in your setup script:
Step 1: Define Your Software List
In the "Installs needed software" section of your script, create a simple bash array (just a structured list) to hold all the packages you want to install. Each package gets its own line, so you can add/remove items later without messing up the command.
# Installs needed software # Define your list of required packages needed_software=( "git" "vim" "htop" "curl" "firefox" # Add more packages here—one per line, wrapped in quotes )
Step 2: Run the Install Command Using the List
Right below your list, add this command to tell apt-get to install every item in your array. The -y flag skips the "are you sure?" prompt, so your script runs fully automatically without needing manual input.
# Update package lists first, then install all software from the list sudo apt-get update && sudo apt-get install -y "${needed_software[@]}"
Quick, No-Jargon Breakdown
"${needed_software[@]}": This pulls every item from your list and feeds it to the install command—like expanding the list into all the package names at once.sudo apt-get update: Always run this first to make sure your system has the latest info about available packages (prevents "package not found" errors).&&: Only runs the install command if the update succeeds, so you don't waste time trying to install with outdated package data.
Optional: Add Custom Repos (If Needed)
If some software isn't available in Ubuntu's default repositories, you can add a PPA (Personal Package Archive) right before the install step. For example:
# Optional: Add a PPA for software not in default repos (example only) # sudo add-apt-repository -y ppa:webupd8team/sublime-text-3
Just remove the # (uncomment the line) and replace the PPA link with the one for your specific software.
内容的提问来源于stack exchange,提问作者Justin Keith

