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
- $SHELL is to tell you what the current user has but you can change on the fly so that is why the other command is more useful.
- Borne shell (sh) does not play as nicely with arrays. You have to use eval.
- change your default shell to bash. Ref: https://www.tecmint.com/change-a-users-default-shell-in-linux/