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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:20:34+00:00 2026-06-05T11:20:34+00:00

Context: We have several pieces of our infrastructure that are managed by large sets

  • 0

Context:

We have several pieces of our infrastructure that are managed by large sets of Bash scripts that interact with each other via the source command, including (and frequently chaining includes) of files that are created compliant with a standard Bash template we use. I know that this is a situation that should probably never have been allowed, but it’s what we have.

The template basically looks like this:

set_params() {
    #for parameters in a file that need to be accessed by other methods 
    #in that file and have the same value from initialization of that 
    #file to its conclusion:
    global_param1=value 

    #left blank for variables that are going to be used by other methods 
    #in the file, but don't have a static value assigned immediately:
    global_param2= 
}

main_internals() {
    #user-created code goes here.
}

main() {
    set_params
    #generic setup stuff/traps go here
    main_internals arg arg arg
    #generic teardown stuff goes here
}

Using this structure, we have files include other files via the source command and then call the included files main methods, which wraps and modularizes most operations well enough.

Problem:

Some of the thorniest problems with this infrastructure arise when a new module is added to the codebase that uses a global variable name that is used somewhere else, unrelatedly, in the same sourced chain/set of files. I.e if file1.sh has a variable called myfile which is uses for certain things, then sources file2.sh, and then does some more stuff with myfile, and the person writing file2.sh doesn’t know that (in many cases they can’t be expected to–there are a lot of files chained together), they might put a non-local variable called myfile in file2.sh, changing the value in the variable with the same name in file1.sh

Question:

Assuming that global variable name conflicts will arise, and that localing everything can’t completely solve the problem, is there some way to programmatically unset all variables that have been set in the global scope during the execution of a particular function or invocations below that? Is there a way to unset them without unsetting other variables with the same names that are held by files that source the script in question?

The answer might very well be “no”, but after looking around and not finding much other than “keep track of variable names and unset anything after you’re done using it” (which will inevitably lead to a costly mistake), I figured I’d ask.

Phrased another way: is there a way to make/hack something that works like a third scope in Bash? Something between “local to a function” and “visible to everything running in this file and any files sourced by this one”?

  • 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-05T11:20:35+00:00Added an answer on June 5, 2026 at 11:20 am

    The following is untested.

    You can save a lot of your variables like this:

    unset __var __vars
    saveIFS=$IFS
    IFS=$'\n'
    __vars=($(declare -p))
    IFS=$saveIFS
    

    or save them based on a common prefix by changing the next to last line above to:

    __vars=($(declare -p "${!foo@}"))
    

    Then you can unset the ones you need to:

    unset foo bar baz
    

    or unset them based on a common prefix:

    unset "${!foo@}"
    

    To restore the variables:

    for __var in "${__vars[@]}"
    do
        $i
    done
    

    Beware that

    • variables with embedded newlines will do the wrong thing
    • values with whitespace will do the wrong thing
    • if the matching prefix parameter expansion returns an empty result, the declare -p command will return all variables.

    Another technique that’s more selective might be that you know specifically which variables are used in the current function so you can selectively save and restore them:

    # save
    for var in foo bar baz
    do
        names+=($var)
        values+=("${!var}")
    done
    
    # restore
    for index in "${!names[@]}"
    do
        declare "${names[index]}"="${values[index]}"
    done
    

    Using variable names instead of “var”, “index”, “names” and “values” that are unlikely to collide with others. Use export instead of declare inside functions since declare forces variables to be local, but then the variables will be exported which may or may not have undesirable consequences.

    Recommendation: replace the mess, use fewer globals or use a different language.

    Otherwise, experiment with what I’ve outlined above and see if you can make any of it work with the code you have.

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

Sidebar

Related Questions

I have several BusinessObject classes that refer to each other and I need to
I have several Div's that appear and would like each one to have a
I have several UITextFields listed below each other and would like to move the
I have several different content type nodes (videos, image galleries, stories...) that I would
We have a Flex application which has several 'pages' worth of content. Each time
I have a index.php file that will include several external files: content/templates/id1/template.php content/templates/id2/template.php content/templates/id3/template.php
I have a class that extends RemoteServiceServlet. This class has several methods, in one
i have several AreaRegistration classes which one each registers own routes and each one
On a page I am working on, I have several distinct pieces of PHP,
I have several dependency injection services which are dependent on stuff like HTTP context.

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.