Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-12-2010, 04:04 PM   #1
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
[ -d mydir ]
cd mydir/ && pwd

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

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
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.


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 + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Old 07-12-2010, 09:16 PM   #5
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


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 > Forums > Linux Forums > Linux - Newbie

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

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