I'm writing a small script to test my regex understanding of comparison operator "=~". I thought that my syntax was alright but I keep getting:
3: Syntax error: "(" unexpected
this is my small script link to this syntax error :
#!/bin/bash
inputsArr=("ab" "67" "7b7" "g" "67777" "07x7g7" "77777" "7777" "")
for input in ${inputsArr[@]}; do
if [[ "$1" =~ "$input" ]]; then
echo "$?"
fi
done
I try to compare in a loop with an array some "strings" against my arg1 or "$1"
dashinstead ofbash../your_script.shbut withsh your_script.sh. On ths way you useshand notbash.shdoes not know arrays. See:sh(Bourne-shell) is usally notbash(Bourne-again shell).${inputsArr[@]}as well as regular variable references, to prevent the shell from mangling the array elements (via word splitting and filename wildcard expansion). On the other hand, if$inputis supposed to be treated as a regular expression rather than a fixed string, you should remove the double-quotes around it (note: this is specific to how the right-hand side of a[[ =~ ]]comparison works). shellcheck.net is good at pointing out common mistakes like these.