0

as the title say, I have many variables, like 200, and I'd like to have a script just containing the declarations, appart from my bash with execute code. Would it be possible with my bash that execute code to just call a script that create the variable and that exist after?

EDIT : The site proposed that answer the questions explain the same situation, however there are really good details the people who answered here gave.

Cher
  • 2,789
  • 10
  • 37
  • 64
  • possible duplicate of [Bash script - How to reference a file for variables](http://stackoverflow.com/questions/5228345/bash-script-how-to-reference-a-file-for-variables) – rahul May 21 '15 at 20:45

2 Answers2

2

When the declarations are in /usr/local/lib/myvars, start your script (after the SHEBANG line) with sourcing that file using the dot notation:

. /usr/local/lib/myvars

When you have so much vars, you must have a lot of code and some general functions. Put those in one file and include that one:

. /usr/local/lib/my_utils

And know you might be wondering: 2 includes in every scriptfile? No, you can source the myvars file in the my_utils file.

Be aware you are introducing global variables, they can be changed everywhere.

Walter A
  • 19,067
  • 2
  • 23
  • 43
2

You can export the variables to be available externally and source that file.

Example. Contents of variables.sh

export VARIABLE1=Value1
export VARIABLE2=Value2
.
.
export VARIABLE200=Value200

Contents of main script:

#!/bin/bash

source /Pathtosourcefile/variables.sh

echo $VARIABLE1

This would print out:

Value1
rahul
  • 304
  • 2
  • 13
  • The variables don't necessarily need to be exported. Once sourced, they will be shell variables just as if they had been declared directly in the script itself. `export` is only needed if the variable needs to be visible in a child process started by the script. – chepner May 21 '15 at 20:37