2

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"

4
  • 1
    That looks somewhat (though not exactly) like the error you would get if you ran the script with dash instead of bash. Commented Mar 21, 2022 at 20:10
  • Btw.: Please paste your script at shellcheck.net and try to implement the recommendations made there. Commented Mar 21, 2022 at 20:33
  • 2
    I suspect that you are not calling the script with ./your_script.sh but with sh your_script.sh. On ths way you use sh and not bash. sh does not know arrays. See: sh (Bourne-shell) is usally not bash (Bourne-again shell). Commented Mar 21, 2022 at 20:36
  • 1
    Side note: you should double-quote ${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 $input is 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. Commented Mar 21, 2022 at 21:37

2 Answers 2

3

The code works in bash, you just need to run it in the right shell, you can do the following:

  bash ./script.sh g

Also type ps -p $$ (not echo $SHELL) to see what shell you are currently in:

Examples:

# ps -p $$
    PID TTY          TIME CMD
  25583 pts/0    00:00:00 sh
# exit
# ps -p $$
    PID TTY          TIME CMD
  22538 pts/0    00:00:00 bash
Sign up to request clarification or add additional context in comments.

1 Comment

The Bourne shell doesn't do arrays at all; you can sort-of fake them with eval, but unless you know exactly what you're doing and are very careful, it tends to be weirdly buggy. If you need arrays, it's much better to use a shell that actually supports them, like bash, zsh, or ksh.
1

I just reach my goal with this !

#!/bin/bash

inputsArr=("ab" "67" "7b7" "g" "67777" "07x7g7" "77777" "7777" "")

for input in ${inputsArr[@]}; do [[ "$input" =~ $1 ]]; echo "$?" ; done 

I would like to say thanks you to every person that give me some tips on this basic BASH script problem. Without you I would certainly not reach my goal by my own way and it is beautiful to see this cooperation in action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.