Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-20-2012, 02:04 PM
|
#1
|
|
LQ Newbie
Registered: Apr 2012
Location: South Mississippi
Distribution: puppy linux
Posts: 2
Rep: 
|
cd command usage a in shell script using bash
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?
Thanks
|
|
|
|
04-20-2012, 02:44 PM
|
#2
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
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
|
|
|
1 members found this post helpful.
|
04-20-2012, 02:45 PM
|
#3
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,322
|
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  )
|
|
|
1 members found this post helpful.
|
04-20-2012, 02:52 PM
|
#4
|
|
LQ Newbie
Registered: Apr 2012
Location: South Mississippi
Distribution: puppy linux
Posts: 2
Original Poster
Rep: 
|
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.
|
|
|
|
04-20-2012, 03:00 PM
|
#5
|
|
LQ Newbie
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29
Rep:
|
You can also run your script in the current shell, instead of child shell like
but be careful as this will overide all variables.
At the end unset all of the variables you previously created to not make a mess.
|
|
|
|
04-21-2012, 11:44 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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.
http://mywiki.wooledge.org/BashGuide...ands#Functions
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:30 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|