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 8878373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:41:48+00:00 2026-06-14T19:41:48+00:00

The closest solution I came to is: declare -A foobar=([foo]=bar [bar]=foo) (set -u; true

  • 0

The closest solution I came to is:

declare -A foobar=([foo]=bar [bar]=foo)
(set -u; true ${foobar[foo]}) 2>/dev/null

Ideally I would like to use test -v to test if an array key is defined. But test -v foobar[bar] apparently always returns 1. Also I don’t want a global set +u and run the risk to access an undefined variable.

  • 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-14T19:41:49+00:00Added an answer on June 14, 2026 at 7:41 pm

    As of bash version “4.2.39(1)-release” I cannot see how to test for the presence of an array key with the test -v command. test -v foobar[foo] simply always fails. To me the best option looks like one should test with the ${foobar[foo]-bar} parameter substitution. In order to re-test Bash’s behavior with shopt’s nounset shell option and every new release I wrote the following Bash shell script:

    #!/bin/bash
    
    shopt -os nounset
    
    declare -ri colors=1
    if (( "$colors" )); then
      declare -r bold="\e[1m"
      declare -r red="\e[41m"
      declare -r green="\e[42m"
      declare -r end="\e[0;m"
    else
      declare -r bold=
      declare -r red=
      declare -r green=
      declare -r end=
    fi
    declare -ri a="${1:-0}"
    case "$a" in
      1)
        echo -e "$a. Accessing ${bold}\"\$foobar\"${end} when unset should result in an error."
        echo "$foobar"
        ;;
      2)
        echo -e "$a. Accessing ${bold}\"\${foobar-bar}\"${end} when unset should result in the \"bar\" default value."
        echo "${foobar-bar}"
        ;;
      3)
        echo -e "$a. Accessing ${bold}\"\${foobar:-bar}\"${end} when unset should result in the \"bar\" default value."
        echo "${foobar:-bar}"
        ;;
      4)
        echo -e "$a. Accessing ${bold}\"\$foobar\"${end} when set to the NULL string should result in it (whitespace)."
        declare -r foobar=
        echo "$foobar"
        ;;
      5)
        echo -e "$a. Accesssing ${bold}\"\${foobar-bar}\"${end} when set to the NULL string should result in it (whitespace)."
        declare -r foobar=
        echo "${foobar-bar}"
        ;;
      6)
        echo -e "$a. Accesssing ${bold}\"\${foobar:-bar}\"${end} when set to the NULL string should result in the \"bar\" default value."
        declare -r foobar=
        echo "${foobar:-bar}"
        ;;
      7)
        echo -e "$a. Accessing ${bold}\"\$foobar\"${end} when set to the \"foo\" string should result in it."
        declare -r foobar=foo
        echo "$foobar"
        ;;
      8)
        echo -e "$a. Accessing ${bold}\"\${foobar-bar}\"${end} when set to the \"foo\" string should result in it."
        declare -r foobar=foo
        echo "${foobar-bar}"
        ;;
      9)
        echo -e "$a. Accessing ${bold}\"\${foobar:-bar}\"${end} when set to the \"foo\" string should result in it."
        declare -r foobar=foo
        echo "${foobar:-bar}"
        ;;
      10)
        echo -e "$a. Accessing ${bold}\"\${foobar[foo]}\"${end} when unset should result in an error."
        declare -rA foobar=()
        echo "${foobar[foo]}"
        ;;
      11)
        echo -e "$a. Accessing ${bold}\"\${foobar[foo]-bar}\"${end} when unset should result in the \"bar\" default value."
        declare -rA foobar=()
        echo "${foobar[foo]-bar}"
        ;;
      12)
        echo -e "$a. Accessing ${bold}\"\${foobar[foo]:-bar}\"${end} when unset should result in the \"bar\" default value."
        declare -rA foobar=()
        echo "${foobar[foo]:-bar}"
        ;;
      13)
        echo -e "$a. Accessing ${bold}\"\${foobar[foo]}\"${end} when set to the NULL string should result in it (whitespace)."
        declare -rA foobar=([foo]=)
        echo "${foobar[foo]}"
        ;;
      14)
        echo -e "$a. Accessing ${bold}\"\${foobar[foo]-bar}\"${end} when set to the NULL string should result in it (whitespace)."
        declare -rA foobar=([foo]=)
        echo "${foobar[foo]-bar}"
        ;;
      15)
        echo -e "$a. Accessing ${bold}\"\${foobar[foo]:-bar}\"${end} when set to the NULL string should result in the \"bar\" default value."
        declare -rA foobar=([foo]=)
        echo "${foobar[foo]:-bar}"
        ;;
      16)
        echo -e "$a. Testing with ${bold}\"test -v foobar\"${end} when unset should fail."
        test -v foobar
        ;;
      17)
        echo -e "$a. Testing with ${bold}\"test -v foobar\"${end} when set to the NULL string should succeed."
        declare -r foobar=
        test -v foobar
        ;;
      18)
        echo -e "$a. Testing with ${bold}\"test -v foobar\"${end} when set to the \"bar\" string should succeed."
        declare -r foobar=
        test -v foobar
        ;;
      19)
        echo -e "$a. Testing with ${bold}\"test -v foobar[foo]\"${end} when unset should fail."
        declare -rA foobar=()
        test -v foobar[foo]
        ;;
      20)
        echo -e "$a. Testing with ${bold}\"test -v foobar[foo]\"${end} when set to the NULL string should fail."
        declare -rA foobar=([foo]=)
        test -v foobar[foo]
        ;;
      21)
        echo -e "$a. Testing with ${bold}\"test -v foobar[foo]\"${end} when set to the \"bar\" string should succeed."
        declare -rA foobar=([foo]=bar)
        test -v foobar[foo]
        ;;
      0)
        echo -e "Testing shopt's \"nounset\" shell option with Bash version \"$BASH_VERSION\"."
        echo
        declare -i b
        for ((b=1; b<=21; b+=1)); do
          "$0" "$b"
          if (( "$?" )); then
            echo -e "${red}Failure.${end}"
          else
            echo -e "${green}Success.${end}"
          fi
          echo
        done
        ;;
    esac
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask for 3 information here: There is no integrated solution
I would like to create a 3D model using software development-like collaboration. Ideally, I'd
I asked a similar question earlier and we came down to this CSS solution
I would like to ask a little attention to this challenge. My intention is
I would like to programmatically export my Procedures / Functions and Packages into individual
i want to target closest div from my anchor tag. so in order do
What is the closest you can get to a try-catch block in php4? I'm
parentId=$(this).closest('tr').find('td:nth(6)')[0].textContent; I am trying to get ID from flexigrid column. It works fine in
I need to search for the closest Dialcode bigger than the actual one. For
I am trying to find a closest match for a word by giving a

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.