Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:14:36+00:00 2026-06-17T09:14:36+00:00

I was wondering if there is an efficient way to check if an element

  • 0

I was wondering if there is an efficient way to check if an element is present within an array in Bash? I am looking for something similar to what I can do in Python, like:

arr = ['a','b','c','d']

if 'd' in arr:
    do your thing
else:
    do something

I’ve seen solutions using associative array for bash for Bash 4+, but I am wondering if there is another solution out there.

Please understand that I know the trivial solution is to iterate in the array, but I don’t want that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:14:37+00:00Added an answer on June 17, 2026 at 9:14 am

    You could do:

    if [[ " ${arr[*]} " == *" d "* ]]; then
        echo "arr contains d"
    fi
    

    This will give false positives for example if you look for “a b” — that substring is in the joined string but not as an array element. This dilemma will occur for whatever delimiter you choose.

    The safest way is to loop over the array until you find the element:

    array_contains () {
        local seeking=$1; shift
        local in=1
        for element; do
            if [[ $element == "$seeking" ]]; then
                in=0
                break
            fi
        done
        return $in
    }
    
    arr=(a b c "d e" f g)
    array_contains "a b" "${arr[@]}" && echo yes || echo no    # no
    array_contains "d e" "${arr[@]}" && echo yes || echo no    # yes
    

    Here’s a “cleaner” version where you just pass the array name, not all its elements

    array_contains2 () { 
        local array="$1[@]"
        local seeking=$2
        local in=1
        for element in "${!array}"; do
            if [[ $element == "$seeking" ]]; then
                in=0
                break
            fi
        done
        return $in
    }
    
    array_contains2 arr "a b"  && echo yes || echo no    # no
    array_contains2 arr "d e"  && echo yes || echo no    # yes
    

    For associative arrays, there’s a very tidy way to test if the array contains a given key: The -v operator

    $ declare -A arr=( [foo]=bar [baz]=qux )
    $ [[ -v arr[foo] ]] && echo yes || echo no
    yes
    $ [[ -v arr[bar] ]] && echo yes || echo no
    no
    

    See 6.4 Bash Conditional Expressions in the manual.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering is there any way I can check whether the url links to
I'm just wondering if there is any simple/efficient way to check if square falls
I'm wondering if there's a super-efficient way of confirming that an Image object references
I was wondering whether there is an efficient way to retrieve the children of
I was wondering if there is a more efficient way of doing what I
I'm just wondering if there is a more efficient way of doing this. I
I'm developing my first EclipseLink J2SE project, and wondering if there's an efficient way
I was wondering if there is a more efficient way of doing an insert
I am wondering if there is a more efficient way of swapping two elements
I was wondering whether there is a more pythonic (and efficient) way of doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.