What I am doing is this:

I would like to divide my script into multiple chunks. What would the best practice be for defining all of my variables in one place? Should I define them all in the init script and export or is there a better way to have each piece of my script look to one static variables file? Maybe in JSON or the like? Here's an example



a_list_of_packages="pyhon3 nmap apache2.... others"
some_other_info_for_another_script="important info"

menu () {
    read # blah blah whatcha wanna run?
    # other bits
opts () {
    case ... # a bunch of options
    1) ./src/another_script.sh # one of a bunch of scripts in a separate folder
while true

Now in the ./src/another_script.sh script, what is the best way to deal with the vars for ease and jump-around-ability? (that's a technical term)

  • You use the term "variable" (which is the correct technical term), but you're really talking about constants, right? You don't plan to ever change the value of new_user or some_other_info while the software is running, do you? – G-Man Sep 8 '16 at 4:16

Create an independent file with only the variable declarations:

a_list_of_packages="pyhon3 nmap apache2.... others"
some_other_info_for_another_script="important info"

Then include this file at the beginning in all your scriptfiles with source /my/variables

  • @lpor That is exactly what I'm after! Thanks! I can't give you credit for another few min (you answered so quickly) but I will once the restriction is lifted. Thanks again – archae0pteryx Sep 7 '16 at 22:53

I have to plus one the original answer with some potentially helpful improvements. Some of the information is dependent on the intended user of your scripts, some is general knowledge regarding bash variables, and some is from my own experience.

The directory you store your scripts (~/bin) and/or the directory you hold your variable declarations (~/lib) can be quickly referenced with a slight mod to your PATH environment variable.

Variables are specific to the environment they are called from. Meaning, if you export them, they become globally available for your use, but for this context, only to your login environment. If you change them inside a script, they only hold that value while your script is running.

In my .bashrc I define and export VAR="login variable". This is sourced every time I login to my solaris host.

Now in this example script that I call proof.bash, I will modify VAR in the global context of the script, then again in a function within the script. This does not change the value of the variable after the script is executed.

# VAR is usable because it was declared/exported outside this script.
echo $VAR
#Define bash global variable
#This variable is global and can be used anywhere in this bash script
VAR="global variable"
function see_it_to_believe {
#Define bash local variable
#This variable is local to bash function only
local VAR="local variable"
echo $VAR
echo $VAR
# Note the bash global variable did not change
# "local" is bash reserved word
echo $VAR

After the script has run, I can echo $VAR and get "login variable" yet.

  • 1
    Thank you @daniel I learned a lot from your explanation! – archae0pteryx Sep 7 '16 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.