LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   UNIX processes (https://www.linuxquestions.org/questions/linux-newbie-8/unix-processes-68899/)

joseamuniz 06-29-2003 06:26 PM

UNIX processes
 
Quote:

Understanding that a new process is created when you run a shell script helps to explain a very common misconception under UNIX. When you run a shell script and that script changes directories, your original shell knows nothing about the change. This confuses a lot of people who are new to UNIX as they come from the DOS world, where changing the directory from within a batch file does change the original shell. This is because DOS does not have the same concept of a process as UNIX does.
Extracted from here

I read this in a Linux tutorial. And this lead me to think that if I created a function that changed my directory, it would execute the function and still return me to the same place before I ran the script, so I created:

changeDir()
{
cd /
}

and executed it from my home directory. And the result was me getting redirected to /. Am I doing something wrong? What are they referring to if not that? I don't understand. :( I tried searching in Google but found nothing. Thank you very much in advance.

Dark_Helmet 06-29-2003 06:45 PM

Ok, how did you use this function you created? The tutorial mentions running in a shell script. I just tried putting this into a script called test.bash:

Code:

#!/bin/bash

cd /
echo "Changed directory to /"
echo `pwd`

exit 0

If you execute that script from a directory other than root, it should tell you the working directory is /, but when it quits, you'll be in the same directory you started the script from.

This is why: The '#!/bin/bash' at the top says to create a new bash shell and execute the commands within that new shell. This new shell is another process, and not at all related to the shell you started the script from. It's similar to having two terminals open at the same time.

make sense?

joseamuniz 06-29-2003 07:07 PM

Oh... I got it to work. I can't figure out what I had one wrong but I created my script again and ran it and everything ran smoothly. Thanks for your help.

Dark_Helmet 06-29-2003 07:16 PM

Hehehe... typing this:
Code:

. example
isn't doing what you think it's doing... :) At least, that's the impression I get from the beginning of your reply. That command (the period by itself) is an alias for the source command. That WOULD cause you to change directories in the current shell.

As for the PATH environment variable, it is entirely up to you whether to include the current directory or not. I prefer not to, because then I'm guaranteed not to run something accidentally. Only the programs "officially" installed will run, forcing me to be absolutely certain I want to run what's in the current dir.

joseamuniz 06-29-2003 09:55 PM

Yeah I got aware of my stupid reply and edited it :P I didn't know about the alias, though. :) That cleared yet more doubts out. Thanks a bunch.

Dark_Helmet 06-29-2003 10:16 PM

Stupid? Nah. It's those little things that can cause the biggest stumbling blocks. I chuckled only because I had very similar experiences.

Glad I could help.


All times are GMT -5. The time now is 04:35 PM.