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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:25:32+00:00 2026-05-29T09:25:32+00:00

Say I have a bash script file config.sh . It’s meant to be source’d

  • 0

Say I have a bash script file config.sh. It’s meant to be source’d by other scripts and variables defined is used as customization of the upper-level scripts.

The problem is, if config.sh has a temporary variable and its name conflicts with upper-level scripts’ variable, it breaks the upper-level one.

config.sh:

TMP1=abc
CONFIG_INPUT_DIR="$TMP1"/in
CONFIG_OUTPUT_DIR="$TMP1"/out

upper-level script:

TMP1=def
source config.sh
echo $TMP1

The last echo prints abc, not def.

Solution 1

My current solution is to append a random string to the temporary variable name to make it almost impossible to conflict. e.g:

TMP1_vFc9Uiew=abc
CONFIG_INPUT_DIR="$TMP1_vFc9Uiew"/in
CONFIG_OUTPUT_DIR="$TMP1_vFc9Uiew"/out
unset TMP1_vFc9Uiew

which is painful and makes the code hard to read, in addition not to be perfect.

Solution 2 using local keyword

After some searching, I’ve come to know local keyword.
But when I simply declare TMP1 as local, bash complains that config.sh: line 1: local: can only be used in a function.

So my another solution is to enclose whole config script as a function:

function config_func_rZ0Yqkpm() {
  local TMP1=abc
  CONFIG_INPUT_DIR="$TMP1"/in
  CONFIG_OUTPUT_DIR="$TMP1"/out
}
config_func_rZ0Yqkpm
unset config_func_rZ0Yqkpm

which is better than previous solution in maintainability and readability, but there’s some possibility to conflict as well as solution 1.

Question

I want to know more robust and smart solution without any possibility to conflict.

Thanks.

  • 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-05-29T09:25:33+00:00Added an answer on May 29, 2026 at 9:25 am

    A trick I learned from the keychain utility is using one program to build a source-able file containing just the variables that you want to export from your program. You could modify your script to echo the variables you want to set and then source the output from your program:

    $ echo $FOO
    
    $ source <(echo FOO=bar)
    $ echo $FOO
    bar
    $ 
    

    I used echo FOO=bar to simulate the larger script; your program is probably more involved. The important part is that you must modify your program to output the variables and values you would like to set, rather than just setting them. This lets you decide which variables to expose and which ones to hold private at the cost of another shell process.

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

Sidebar

Related Questions

Say I have the following Bash script stored in the file foo.sh : #!/bin/bash
Say I have a file at the URL http://mywebsite.example/myscript.txt that contains a script: #!/bin/bash
Let's say I have a bash script that executes three scripts in parallel ./script1
Let's say I have a Bash script called foo.sh . I'd like to call
Say I have a bash script as follows while read $f; do cat $f
Lets say i have a python script at homedir/codes/py/run.py I also have a bash
I have a bash script which is getting options (using getopts). Let say they
I have a bash script file which starts with a function definition, like this:
Say i have a bash script: #!/bin/bash php ./listen.php 3001 3003 26 & php
I have a csv file like, let's say table.csv foo;3 bar;1 and a script

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.