LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 04-20-2012, 02:04 PM   #1
chuckt6
LQ Newbie
 
Registered: Apr 2012
Location: South Mississippi
Distribution: puppy linux
Posts: 2

Rep: Reputation: Disabled
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
 
Old 04-20-2012, 02:44 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
Old 04-20-2012, 02:45 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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.
Old 04-20-2012, 02:52 PM   #4
chuckt6
LQ Newbie
 
Registered: Apr 2012
Location: South Mississippi
Distribution: puppy linux
Posts: 2

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-20-2012, 03:00 PM   #5
yoK0
LQ Newbie
 
Registered: Apr 2012
Distribution: Slackware, CentOS
Posts: 29

Rep: Reputation: 0
You can also run your script in the current shell, instead of child shell like

Code:
. myScript
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.
 
Old 04-21-2012, 11:44 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash][awk] command runs from shell, not from script JohnyDRipper Programming 2 03-09-2012 05:01 AM
LXer: Command Substitution in a Bash Shell Script LXer Syndicated Linux News 0 02-03-2011 07:30 PM
usage of history command inside shell script bsaheb Linux - Newbie 4 01-05-2010 08:55 PM
Command that works on BASH promp but not from a shell script! ChrisScott Linux - General 4 11-27-2006 07:11 PM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration