Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
2 and 3 are the same, it means the program will be executed by the current shell. Tcsh cannot understand 2) some other shells cannot understand 3).
1 means a new shell will be opened (child process) and that will execute foo.
The main difference is that using method 1 you can execute a script but your current shell will remain as it was before, using 2 or 3 you will be able to modify your actual environment (define functions, set variables ...)
The '.' notation in bash is just shorthand for 'source'. To 'source' a file is causing the current shell process to read it in mostly the same manner as if you had typed it interactively. This allows you to use that method when you need to alter the properties of the current shell session, such as changing directories or setting environment variables. When a script is 'run', as your "1)" method describes, it launches a child shell which executes the script, and then terminates. Any alteration of process properties is restricted to that child shell. Since the child shell terminates when the script completes execution, those changes vanish with it. Moreover, the child shell (like any process) is incapable of altering the properties of any other process, be it a parent, child or sibling.
Aha... When I have just installed an O.S. I must afterwards do some settings and install some software. For instance, one thing I do, is to install two programs: autojump and ytree. To save work, I could write a script like this:
Code:
#!/bin/sh
## Linked to /bin/bash in my system.
### Pseudocode - I don't know how this is done
if ~/work does not exist
mkdir ~/work
### Pseudocode END
cp /deb4/almacen/soft/ncd/autojump-v8.tar.gz ~/work
cp /deb4/almacen/soft/ncd/ytree-1.94.tar.gz ~/work
cd ~/work
gzip -dv autojump-v8.tar.gz
gzip -dv ytree-1.94.tar.gz
tar -xvf autojump-v8.tar
tar -xvf ytree-1.94.tar
cd joelthelion-autojump-ea68448
./install.sh
# Here I just edit ~/.bashrc and /root/.bashrc
# when working interactively. So here I do:
echo "source /etc/profile.d/autojump.bash" >> ~/.bashrc
echo "source /etc/profile.d/autojump.bash" >> /root/.bashrc
cd ~/work/ytree-1.94
make
make install
According to what has been said, it would be exactly the same if I source this script or I directly run it (methods 3) and 1) above). Am I right?
I rarely put build commands in scripts though, and I recommend against it as well. Unless you are positive your system is set up properly (all necessary dependencies), it could easily fail. The problem with scripts is, if a command fails, the script just keeps on going. For example, if your "make" fails because of a missing dependency, the script doesn't give a crap and goes straight to "make install". You should really be putting in some error checking if you want this to be a reliable tool.
Last edited by suicidaleggroll; 04-25-2014 at 10:26 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.