bash if string starts with character

Viewed 30

This is very basic but eluding me. I have an array. I am checking each element of this array to see if it starts with given character. I think it what i have written is right but not getting desired response.

My code

arr=("etpass-foo" "etpass-bar" "pass-foo" "pass-abc" "etpass-abc")
for i in "${arr[@]}"
do
     if [[ $i == et* ]]; then
          printf "$i"
     fi
done

I get below output

etpass-foo
etpass-bar
pass-foo
pass-abc
etpass-abc

What is expect is

etpass-foo
etpass-bar
etpass-abc

I have also tried below if conditions

1. if [[ $i == et* ]]; then
2. if [[ "$i" == "et"* ]]; then
3. if [[ "$i" == "et*" ]]; then

Please let me know where i am wrong?

1 Answers

Try to use bashregex instead, like this:

...
if [[ $i =~ ^et.* ]]; then
    echo "$i"
fi
...
Related