Home > Enterprise >  Declaring BASH. Array of strings in a a script
Declaring BASH. Array of strings in a a script

Time:03-22

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"

CodePudding user response:

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
  • Related