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 you have a shell program asdf. what's the difference of running the following commands?
. asdf
or
chmod u+x asdf ; asdf
Both of them execute the file. But lately, I started to notice subtle differences, for example the variable $0 is different. Anyone with any ideas what's the difference between executing a file . asdf or just asdf with the right permissions? Thanks
The period in the front is a more explicit way to run the program; it says "run the executable file called asdf in the current directory." Without the period, your shell (bash or whatever) is depending upon the $PATH environment variable to know where to find the executable called asdf. If the current directory is not in the $PATH, it won't work to run it without the leading period. There's also the chance that another executable in your $PATH is found first; say, if there was a system-wide utility called asdf, and it was in /usr/bin, it's likely to be found (and run) first. Sometimes, a default $PATH setup will not even include your /home directory, so if that's where the asdf program is, it might not work at all to just use 'asdf'.
You can run 'which asdf' to see the full path name of the file that will be run if you just type 'asdf' to run it.
well, that'd be true for './asdf', right? For '. asdf', it's saying to source the file - run it in the current instance. Whereas, with a normal 'asdf' it spawns a subshell to execute. So the environment is going to be different because the subshell isn't a login shell - well, and is just a 'different shell' in general. If the contents of asdf are 'cd; ls' and you run it, you don't go anywhere - your current shell is still where it was when the subshell exits. Whereas if you source it, you will cd and then ls.
If you want, for example, environment variables to stick around after they are changed, you run commands like this:
Code:
. asdf
# or
{ asdf }
This makes the script run in your existing process, not a subprocess.
Creating a subprocess or not creating one is only important when you want changes to things to stick around. If you do not want a cd command to actually change your current working directory after the code is done, then don't use the dot command. run the script in a subprocess.
subprocess is another word for "new shell"
If you put she-bang as the first line of a script you get a new shell
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.