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

  • Home
  • SEARCH
  • 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 8328119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:20:26+00:00 2026-06-09T01:20:26+00:00

I can’t seem to access the value of my loop variable when executing a

  • 0

I can’t seem to “access” the value of my loop variable when executing a command line argument in a Bash script. I’d like to be able to write something like

#!/bin/bash
for myvar in 1 2 3
do
  $@
done

and run the script as ./myscript echo "${myvar}".

When I do this, the lines are echoed as empty. I probably don’t have a firm grasp one exactly what’s being evaluated where.

Is what I want even possible?

  • 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-09T01:20:27+00:00Added an answer on June 9, 2026 at 1:20 am

    $myvar is evaluated before the child script is even run, so it can’t be evaluated within.

    That is, when you invoke your script as:

    ./myscript echo "${myvar}"
    

    what is actually being called is:

    ./myscript echo ''
    

    presuming that $myvar is empty in the enclosing environment.


    If you wanted to be evil (and this is evil, and will create bugs, and you should not do it), you could use eval:

    #!/bin/bash
    for myvar in 1 2 3; do
      eval "$1"
    done
    

    …and then call as:

    ./myscript 'echo "${myvar}"'
    

    Something less awful would be to export a variable:

    #!/bin/bash
    export myvar
    for myvar in 1 2 3; do
      "$@"
    done
    

    …but even then, the way you call the script will need to avoid preevaluation. For instance, this will work in conjunction with the above:

    ./myscript bash -c 'echo "$myvar"'
    

    …but that basically has you back to eval. On the other hand:

    ./myscript ./otherscript
    

    …will do what you want, presuming that otherscript refers to $myvar somewhere within. (This need not be a shell script, or even a script; it can be an executable or other command, and it will still be able to find myvar in the environment).


    What’s the real goal (ie. business purpose) you’re trying to achieve? There’s probably a way to accomplish it better aligned with best practices.

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

Sidebar

Related Questions

Can I run this in a Windows command prompt like I can run it
Can anyone let me know how can we change the value of kendo combobox
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can we close all known/unknown connections to database with the code? I'm using Access
Can't seem to get the Back Button to appear in a UINavigationController flow. I
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can anyone tell me how I can display a status message like 12 seconds
Can a DateTime value be NULL ? I have this code: From here I
Can I order my users in the database, so I don't have to say

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.