Bash - loop to test string - syntax error  [Solved]

Here you can exchange scripts that you created or have permission to share with other users.
Message
Author
Sprinterdriver
Posts: 7
Joined: Wed Oct 06, 2021 2:02 pm

Bash - loop to test string - syntax error

#1 Post by Sprinterdriver »

Hi, noob here.

I'm attempting to create a simple (test) script that are supposed to update some flatpak apps. However, I'm stuck on a basic issue - to get the loop to work.

Code: Select all

#!/bin/bash

# Declare an array of strings
lista = ( "org.freecad.FreeCAD" "vn.hoabinh.quan.CoBang" "net.giuspen.cherrytree" "org.avidemux.Avidemux" "com.xnview.XnViewMP" "com.rawtherapee.RawTherapee" )


for item in "${lista[@]}"; do
    echo "flatpak update $item"
    echo ""
done
But when attempting to run the script, I get the following error in terminal:

Code: Select all

$ bash loop_list.sh
loop_list.sh: line 4: syntax error near unexpected token `('
loop_list.sh: line 4: `lista = ( "org.freecad.FreeCAD" "vn.hoabinh.quan.CoBang" "net.giuspen.cherrytree" "org.avidemux.Avidemux" "com.xnview.XnViewMP" "com.rawtherapee.RawTherapee" )'
I've made sure the script are executable.

What is wrong with the list declaration in my script ?

Code: Select all

System:
  Kernel: 6.1.0-17-amd64 [6.1.69-1] arch: x86_64 bits: 64 compiler: gcc v: 12.2.0
    parameters: BOOT_IMAGE=/boot/vmlinuz-6.1.0-17-amd64 root=UUID=<filter> ro quiet splash
  Desktop: KDE Plasma v: 5.27.5 wm: kwin_x11 vt: 7 dm: SDDM Distro: MX-23.6_KDE_x64 Libretto
    January 21 2024 base: Debian GNU/Linux 12 (bookworm)

User avatar
DukeComposed
Posts: 1506
Joined: Thu Mar 16, 2023 1:57 pm

Re: Bash - loop to test string - syntax error  [Solved]

#2 Post by DukeComposed »

Sprinterdriver wrote: Sat Aug 16, 2025 6:17 am What is wrong with the list declaration in my script ?
Spacing. Try running this and compare.

Code: Select all

#!/usr/bin/env bash
exec 2>&1

thing=( "a" "b" "c" )
for i in "${thing[@]}"; do
  echo $i;
done

User avatar
Adrian
Developer
Posts: 9216
Joined: Wed Jul 12, 2006 1:42 am

Re: Bash - loop to test string - syntax error

#3 Post by Adrian »

Bash doesn't like spaces around =
It's actually annoying because it's nicer to leave spaces around operators, and it's not consistent with ( and [ either where you do have to leave space.

DeepDayze
Posts: 179
Joined: Tue Jan 18, 2022 3:34 pm

Re: Bash - loop to test string - syntax error

#4 Post by DeepDayze »

Adrian wrote: Sat Aug 16, 2025 9:07 am Bash doesn't like spaces around =
It's actually annoying because it's nicer to leave spaces around operators, and it's not consistent with ( and [ either where you do have to leave space.
yeah it sure is a bit of an annoyance with Bash and hopefully a future update would make the = be like ( and [ but space ought to be optional around = unlike the other 2 operators.
Real Men Use Linux

User avatar
operadude
Posts: 1072
Joined: Tue Nov 05, 2019 12:08 am

Re: Bash - loop to test string - syntax error

#5 Post by operadude »

Thanks for all that !

I've been brushing-up on my Bash skills, and this is perfect timing !

The script offered by @DukeComposed uses:

Code: Select all

#!/usr/bin/env bash
and I wondered about that, until I found in the tutorial I have been using:

"Links often make sure that both /usr/bin/bash and /bin/bash work. A general-purpose hash bang formulation that works regardless of location uses the /usr/bin/env command. This command searches all common locations: #!/usr/bin/env bash."

Thanks ya' all...and good to know !!!

Off-topic (sort-of): Is the space-around-the-equals-sign thing also in issue in Zsh ?

Sprinterdriver
Posts: 7
Joined: Wed Oct 06, 2021 2:02 pm

Thanks - that was the solution

#6 Post by Sprinterdriver »

Thanks for solution, I forgot about the no-space-around-equalsign rule in bash

Post Reply

Return to “Scripts”