Why must I not quote a string variable when running it as a command?
4
votes
2
answers
702
views
My Ubuntu/Debian-based Linux update POSIX shell script seems to require that I **not double quote** the string variable with the stored command, which is being executed. As I don't understand this issue, I'd like to ask why that is? And if the code is correct as it is?
Warning: SC2086, wiki , "Double quote to prevent globbing and word splitting."
The script follows, the problematic part is highlighted:
#!/bin/sh
# exit script when it tries to use undeclared variables
set -u
# color definitions
readonly red=$(tput bold)$(tput setaf 1)
readonly green=$(tput bold)$(tput setaf 2)
readonly yellow=$(tput bold)$(tput setaf 3)
readonly white=$(tput bold)$(tput setaf 7)
readonly color_reset=$(tput sgr0)
# to create blocks of texts, I separate them with this line
readonly block_separator='----------------------------------------'
step_number=0
execute_jobs ()
{
while [ ${#} -gt 1 ]
do
job_description=${1}
job_command=${2}
step_number=$(( step_number + 1 ))
printf '%s\n' "Step #${step_number}: ${green}${job_description}${color_reset}"
printf '%s\n' "Command: ${yellow}${job_command}${color_reset}"
printf '%s\n' "${white}${block_separator}${color_reset}"
# RUN THE ACTUAL COMMAND
# ShellCheck warns me I should double quote the parameter
# If I did, it would become a string (I think) and I'd get 'command not found' (proven)
# As I don't understand the issue, I left it as I wrote it, without quotes
### shellcheck disable=SC2086
if sudo ${job_command} #
then
printf '\n'
else
printf '%s\n\n' "${red}An error occurred.${color_reset}"
exit 1
fi
shift 2
done
}
execute_jobs \
'configure packages' 'dpkg --configure --pending' \
'fix broken dependencies' 'apt-get --assume-yes --fix-broken install' \
'update cache' 'apt-get update' \
'upgrade packages' 'apt-get --assume-yes upgrade' \
'upgrade packages with possible removals' 'apt-get --assume-yes dist-upgrade' \
'remove unused packages' 'apt-get --assume-yes --purge autoremove' \
'clean up old packages' 'apt-get autoclean'
Asked by Vlastimil Burián
(30515 rep)
May 10, 2019, 08:54 AM
Last activity: Nov 10, 2022, 08:28 AM
Last activity: Nov 10, 2022, 08:28 AM