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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:57:15+00:00 2026-06-14T20:57:15+00:00

I’m looking for a clean way (without eval command) to do an indirect reference

  • 0

I’m looking for a clean way (without eval command) to do an indirect reference to an array. Here is a more precise description of what I want :

function valueof {
   echo "indirection1 \$$1=${!1}"
   eval "echo indirection2 \\\$$1=\${$1[@]}" # Untill this step its fine.

   # My final objective is to do this (but eval is not a very sexy solution) : 
   for i in $(eval "echo \${$1[@]}") ; do 
      echo $i
   done

   # Here is the "problem", ie. "bad substitution"
   echo "indirection3 \$$1=${!1[@]}"  
   # "1[@]" is evaluated first i guess?
}

With the following valueof calls :

a=("a" "aa" "aaa")
b=("b" "bb" "bbb")
valueof a
valueof b

The output I have is :

indirection1 $a=a
indirection2 $a=a aa aaa
a
aa
aaa
indirection1 $b=b
indirection2 $b=b bb bbb
b
bb
bbb

On stderr :

prog.sh: line 10: indirection3 $1=${!1[@]}: bad substitution
prog.sh: line 10: indirection3 $1=${!1[@]}: bad substitution

Thanks for your answers/remarks on this question 🙂

  • 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-14T20:57:16+00:00Added an answer on June 14, 2026 at 8:57 pm

    I suggest building Bash from the git devel branch and using typeset -n like most other sane shells with arrays do. All other solutions involving functions and arrays require either eval or exploiting quirky undocumented behaviours. Both require about equal care and there isn’t necessarily an advantage to one over the other.

    Here is a general example which demonstrates just about everything you can do indirectly without eval. Namespace collisions are still possible.

    isSubset() {
        local -a 'xkeys=("${!'"$1"'[@]}")' 'ykeys=("${!'"$2"'[@]}")'
        set -- "${@/%/[key]}"
    
        (( ${#xkeys[@]} <= ${#ykeys[@]} )) || return 1
    
        local key
        for key in "${xkeys[@]}"; do
            [[ ${!2+_} && ${!1} == "${!2}" ]] || return 1
        done
    }
    
    a=(abc def [4]=ghi jkl)
    b=(abc def [4]=ghi jkl)
    c=(abc [3]=def [6]=ghi xyz)
    isSubset a b
    echo $? # 0
    isSubset b c
    echo $? # 1
    

    This is really eval in disguise in some respects. Most people are unaware that they are effectively performing eval any time they pass variable names to builtins and arithmetic expressions. You have to always ensure that variable names and indexes are controlled internally and never influenced by user input or other side-effects that you can’t guarantee the results of.

    Judging by your misuse of wordsplitting and quoting, you should probably just switch to a different language. Bash isn’t really meant to deal with safe encapsulation.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to show the soap response to UIWebview.. my soap response is, <p><img
I have an array which has BIG numbers and small numbers in it. I

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.