Using global variables in multiple shell scripts, how to?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using global variables in multiple shell scripts, how to?
Hi There,
I have few multiple script which are pulling data from password protected http site. Unfortunately those have to get executed separately. Those script are around in number and I have created variable in every script for --http-user & --http-password; can I create global variable which can be then used in those multiple files so that I dont have to keep changing in those 8 scripts.
Perhaps I don't understand your question correctly.
What I am saying is that if you have a variable which you have to declare in 8 separate scripts, and the variable changes frequently, then why don't you put that variable's value into a configuration file and get each script to read the value of the variable from the file. That way you only need to change the contents of the configuration file if the variable's value changes, not any of the 8 scripts.
Edit: I started constructing this reply while you were editing your previous post, and when I finally posted it, it appears to have replaced your original post which I quoted in my reply. An interesting feature, but it makes my post look stupid now.
Search on the internet for bash read variable from file (I'm assuming that you're using Bash, although you haven't specified which scripting language you're using). You could consider sourcing the configuration file.
On the other hand, if all the processes are called by the same parent process, then you could look into creating environment variables in the parent process which are inherited by the child processes. Again, we don't know how you are calling your processes.
but you cannot define them from within those scripts; they would be lost again when the shell is closed.
so you have to define the variables at some earlier point.
e.g.:
in the login shell (on my system, i could do this via /etc/bash.bashrc or ~/.bash_profile, or /etc/profile)
in a parent script, that would in turn call all the other scripts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.