[SOLVED] Shell scripting, difference of "source script.sh" and "./script.sh"
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.
you should not put a script directly in /opt, that's a really bad place to put anything other than other directories.
source does not run the file, it just inserts it into the current script, and evaluates it as part of the current script. This is typically used for adding in standard functions. It doesn't look like your script deserves it's own file anyway, just add it to your path directly.
When you use the "." command, the script is executed by the current
shell. If any variables are assigned, these changes will be visible in
the shell after the script completes
When you execute a script the normal way, a new shell process is
started. Any variable changes will take place in that process, but will
have no effect on the original process.
- If you source a file the export makes the variable present in the current shell and all new shells.
- If you execute a script a new shell is created first. The subsequent export var makes the var available in that new shell and its sub-shells.
It's all about shells. Initially there is only one process running,
and it spawns new processes, including a set of processes that let
you log in.
After you log in, you are running a process with your login shell,
probably bash. When you type most commands, the current shell pauses
while a subshell is created to run the command. After the command
finishes the subshell quits and you are returned to your original shell.
Within your shell you have variables set, and some of them are
exported. The ones that are exported are created in all subshells
automatically. The ones that are not exported do not get created.
So exporting doesn't really make a variable global. It just means
that variable is created in any subshells.
Incidentally, there is one notable command that doesn't create a
subshell, and that is source. When you source a command, the
command is run in the current shell.
If you want your script to work, add "source /opt/path.sh" to
/etc/profile or move the script to /etc/profile.d.
you should not put a script directly in /opt, that's a really bad place to put anything other than other directories
Yes, you're right. I'll move it to /etc.
Quote:
Originally Posted by druuna
- If you source a file the export makes the variable present in the current shell and all new shells.
- If you execute a script a new shell is created first. The subsequent export var makes the var available in that new shell and its sub-shells.
Hope this clears things up a bit.
Yes it did. So I'll have to source the file if I want it to work – or I just add the path in /etc/profile.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.