Scanning array for partial elements in Bash Script

Example of problem:

computerhand=(6H 2C JC QS 9D 3H 8H 4D)
topcard=6D

How do you search ${computerhand[@]} for all elements containing either a "6" or a "D" then
save the output to a file?

This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash.

Any suggestions and advice would be Greatly appreciated.

Thank you,
Cogiz

for e in ${computerhand[@]}; do
    if [[ $e =~ 6|D ]]; then
        echo "$e" >> test.output
    fi
done
#!/bin/bash

computerhand=(6H 2C JC QS 9D 3H 8H 4D)
topcard=6D

search="${topcard:0:1}|${topcard:1:1}"
for e in ${computerhand[@]}; do
    if [[ $e =~ $search ]]; then
        echo "$e" >> test.output
    fi
done
1 Like

thank you very much. It worked like a charm

Cogiz