ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Say I've got a script called blah.sh. Blah.sh has a line:-
source foo.sh && foo.sh
where foo.sh is another script. The whole idea is to have small re-usable scripts and foo.sh is one of them. When I run blah.sh as user root, foo.sh is sourced and run but the shell tells me
./blah.sh: line 23: foo.sh: command not found
even though foo.sh has been run.
Is this reflected the $PATH of the regular user and that of root? (I'm sure that it's related to my $PATH, but I'm not sure how to rectify this - yet)
however, they do not do exactly the same thing. running the script directly, like ./foo.sh, will run it in a subshell. That is probably a detail that doens't make much difference, it never has in anything ive done, but i think it can if the environment variables are important in your script. i was wondering the same thing myself about running it twice...
however, they do not do exactly the same thing. running the script directly, like ./foo.sh, will run it in a subshell. That is probably a detail that doens't make much difference, it never has in anything ive done, but i think it can if the environment variables are important in your script. i was wondering the same thing myself about running it twice...
Your right, it does make shell variables active in the current scope, however, it still doesn't make sense to me in the OP's context. If you have sourced the script, then any executable code has been run. If the executable code depends on the shell variables declared, just declare them before the commands, and it just needs to be sourced, right? Why the source then the strait execution?
I had no idea that `source blah.sh` and `blah.sh` did the same thing. I thought that source blah would (kind of), load a script anywhere in my $PATH and then I'd have to run it with the name of the script....
edited to add:- thanks guys. I chaged the line to `source blah.sh`, the script was executed with no complaints and everything's lovely
Actually, 'source' or short form '. ' ie dot-space includes the contents of the named file directly into the calling file ie it's at the same shell level.
Using ./file.sh will create a sub-shell, so only exported vars from the parent are visible, and any vars created in the subshell cease to exist as soon as it terminates, so you can't use them in the parent shell.
HTH
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.