maybe someone here can enlighten me:
function:
add_repo() {
for repo in "${repos[@]}"; do
sudo flatpak remote-add --if-not-exists "$repo"
done
}
script:
#! /usr/bin/env bash
repos=(
"flathub https://flathub.org/repo/flathub.flatpakrepo"
)
source function
add_repo
output:
sudo flatpak remote-add --if-not-exists 'flathub https://flathub.org/repo/flathub.flatpakrepo'
output without quotation on $repo
:
sudo flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
This is the desired output.
Question:
How can I quote the variable without added single quotes? I know, that set -x
(set only for debugging) adds these single quotes to allow for better reading, but somehow the command ends after --if-not-exists
because of the added single quotes even without set -x
.
CodePudding user response:
When consuming subset groups of values from a list; it is convenient to have a function or a command to pop-out needed values from list's values passed as arguments.
Here, create the addrepos
bash script command, so it can be invoked with sudo
, elevating privileges only once to add all repositories.
#!/usr/bin/env bash
# addrepos NAME URL [ NAME URL ] ...
# While there are arguments
while [ "$#" -gt 0 ]; do
# Pops repository name from arguments
repo_name="${1}"
shift
# Pops repository URL from arguments
repo_url="${1}"
shift
flatpak remote-add --if-not-exists "${repo_name}" "${repo_url}"
done
Make it executable:
chmod x addrepos
The main script call the addrepo
script with sudo
:
#! /usr/bin/env bash
# repository name and url are distinct entries in the repos array
repos=(
'flathub' 'https://flathub.org/repo/flathub.flatpakrepo'
)
# sudo call addrepos with the content of the repos array as arguments
sudo ./addrepos "${repos[@]}"