LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 07-12-2010, 04:04 PM   #1
dukedog
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Rep: Reputation: 0
cd inside if than statement, after fi back to old dir


the script below prints the pwd of where I want to be. after the fi it puts me back at the original dir. just writing these scripts to understand what's going on.


#! /bin/bash
if
[ -d mydir ]
then
cd mydir/ && pwd
fi

thanks again for the great www.site
 
Old 07-12-2010, 04:44 PM   #2
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Hmmmm, I remember something about this.... It is (a little) complicated.

Maybe best to try a search on bash function cd to find some explanations about just why this is happening.

HTH

Last edited by tredegar; 07-12-2010 at 05:19 PM. Reason: typo
 
1 members found this post helpful.
Old 07-12-2010, 05:10 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Is this actually a question?

If you're running this as a stand-alone script, then the shell forks a separate process for it, and all actions taken (other than echoing to stdout) only affect that subshell. Since no sub-process can change the environment of its parent, once the script exits, its local environment is lost and you're back where you started.

If you run the command directly from the command line, or set and run it as a function in the current shell, then it will be executed inside the current process, and will leave you in that directory.
 
1 members found this post helpful.
Old 07-12-2010, 05:51 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,330

Rep: Reputation: 227Reputation: 227Reputation: 227
http://www.google.com/url?sa=t&sourc...ulmIp9g1I3Ma4Q
 
Old 07-12-2010, 09:16 PM   #5
dukedog
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
thanks for the info. Yes that actually was a question, for us Newbie's sometimes we have to ask dumb questions to understand. Thanks again David the great for your explanation, I though there might be something like that going on but wasn't sure
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 04:07 AM
Iptables forwarding from gateway back to the inside network casolorz Linux - Networking 5 02-03-2009 04:18 PM
If statement to check mode of dir in vi radamsiii Programming 4 09-23-2008 11:11 AM
problem using if statement evaluation inside pool robadawb Linux - Server 2 11-28-2007 03:18 PM
Bash assignment - using a grep inside an if statement lowpro2k3 Programming 4 03-25-2005 06:24 PM


All times are GMT -5. The time now is 06:42 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration