1

I have a linux script job that is made up of 7 scripts in total to complete the job. There are about 60 Variables defined at the start of each script. These variables will be constant & identical in each of the 7 scrips.

The problem I have is that when I am working on the scripts, I have to copy paste all variables to all scripts each time I update or modify variable(s)

Is there a way that I can define all the variables in a file "variables.txt" & somehow reference all these variables from variables.txt at the start of each script?

I was thinking of using sed, but there is probably an easier & cleaner way..

1
  • Yes you can. Put the line . variables.txt just below #!/bin/bash in each script and you're done. Make sure they are all in the same directory. Commented Dec 4, 2013 at 4:11

2 Answers 2

1

Create a file with the constants, and use source in each script:

#!/bin/bash
source `dirname $0`/variables.txt

(The dirname $0 part is to ensure that no matter how the script is called, it looks for variables.txt in the same directory as the script.)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that but scripts are not working as they should. Some of the scripts are copied to a temp-working folder to be executed, could that be the problem
As long as variables.txt is in your path, source variables.txt should find it.
Well, can you have the process that copies those scripts also copy varaibles.txt to the same place? If variables.txt is always going to be in a known place, you can just use the whole path: source /path/to/my/variables/file/variables.txt
0

Maybe if you set global variables, all the scripts will recognise it:

#!/bin/bash
FOO=bar
export FOO

Define Global Variables in Bash

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.