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.
When using the cd command in a shell script, I can move down through several directories just fine, do some creation of some folders, and then exit the script. However, I would like to remain at the lowest level of the directory when I exit the script but for some reason I always return to the directory from which I started. I have tried several variations of cd command and none of them "stick" at the exit level.
#!/bin/bash
cd dir1/dir2/dir3/dir4
(make some files in dir4)
(try to stay at dir4 level in the path)
exit
OK, so now I have come out of the editor (gedit) in this case, and chmod and so forth, run the script, but I always find my place in the path where I created the script.
Hope this is coherent enough to follow. Does anyone know what I am trying to do and is this realistic?
The script runs as a child process of the command shell it is called from and all changes it makes to its process environment -- including the current directory -- are lost when it exits.
You can work around this limitation by having the script write its final directory (and nothing else while it is running) to stdout and capture it in the calling script like this:
Code:
cd $( my_script.sh )
Last edited by catkin; 04-20-2012 at 02:45 PM.
Reason: ist -> its
Yes coherent and also the correct behaviour. Remember that executing a script starts a subshell and at no time does the subshell affect the parent (for lots of good reasons you can look up )
Thanks to all that have replied. Will go try this out. May be Monday before this meal is fully digested but I get the meaning of the replies, and understand the lingo, eg: child process, etc.
A better way to understand it is that (to the best of my knowledge) no process can directly affect the environment of another. They are all isolated.
When one process starts another, the "child" takes on a copy of the the parent environment, or at least the values that are exported. But after initialization is complete, no outside process can change anything directly, or vice-versa. Any changes to a process' environment must be made from within that process.
Depending on your final purposes, you might want to consider converting your script into a function in the main shell. Since the function runs in the current environment, any final values will remain after it exits.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.