How do I check if an array contains anything else than a specific value in bash? -
arr=( d d d a) the code below supposed check if contains d. there similar way check if contains else d?
if [[ " ${arr[*]} " == *" d "* ]]; echo "arr contains d" fi
as long you're not using space in array elements can use:
[[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no" testing:
arr=(d d d a) [[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no" array has non-d element arr=(d d d) [[ " ${arr[*]} " == *" "[^d]" "* ]] && echo "array has non-d element" || echo "no" no
Comments
Post a Comment