- Forums
- Bash Scripts
- Bash Script With For Loop Array White Spaces With Hyphen Dash Problem
This Page Contains information about Bash Script With For Loop Array White Spaces With Hyphen Dash Problem By jj in category Bash Scripts with 0 Replies. [5167], Last Updated: Mon Jun 24, 2024
jj
Thu Jun 22, 2023
0 Comments
298 Visits
today i had a situation where i had a loop and it was creating 4 elements instead of 3. for example, this is how i was looping my array:
# INSTALL DEPENCIES
packages=("react-router-dom" "axios" "vite-plugin-mkcert -D" "bootstrap")
for package in ${packages[@]}; do
PACKAGE="y"
if [[ $ORVERIDE == "n" ]]; then
read -p "Do you want to install ${package} y/n: [n] " PACKAGE
fi
if [[ $PACKAGE == "y" ]]; then
npm i $package
set -e
fi
done
OUTPUT:
[0] = react-router-dom
[1] = axios
[2] = vite-plugin-mkcert
[3] = -D
[4] = bootstrap
For some readson it was creating an extra element from vite-plugin-mkcert -D
Solution:
Change From: ${packages[@]}
Change To: "${packages[@]}"
Now it works.