LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-17-2011, 06:55 PM   #1
LloydRice
Member
 
Registered: Jun 2011
Location: Colorado
Distribution: Debian and Ubuntu
Posts: 67

Rep: Reputation: Disabled
Changing directory from a script


I have a very basic question about Linux.

Is there any way to write a script than can change the directory and leave it changed when the script exits?

Let's say have a file, let's call it 'change-dir':
cd xxx
pwd

Let us assume the file has run permissions, say 0755.

At the prompt, I can type
cd yyy
pwd

and it replies 'yyy'.
If I now execute the above file, it prints
xxx

In other words,
pwd says yyy
change-dir says xxx
pwd says yyy

How can it get it to stay in xxx?

For example, one possibility that occurs to me is that one might be able to alter the directory list used by popd. Then one might be able to execute a popd command and effectively switch to the desired directory. I assume that would require su permissions, but I believe that should be possible.

Is there an easier way to do this directly from the shell?

Last edited by LloydRice; 09-17-2011 at 07:08 PM. Reason: elaboration
 
Old 09-17-2011, 07:20 PM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You can invoke the script in such a way that environment changes (including cd) are made in the current shell. You do this by specifying a leading dot and space before the script. For example:
Code:
. change-dir
 
1 members found this post helpful.
Old 09-17-2011, 07:37 PM   #3
LloydRice
Member
 
Registered: Jun 2011
Location: Colorado
Distribution: Debian and Ubuntu
Posts: 67

Original Poster
Rep: Reputation: Disabled
Excellent. Thanks, macemoneta.
 
Old 09-18-2011, 05:20 AM   #4
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
Please use [code][/code] tags around your code, to preserve formatting and to improve readability.

This issue really requires a bit of explanation.

The point to remember is that changes to a shell's environment always affect only that shell.

When you run a subshell/sub-process, it will make a copy of the containing shell's environment. Other commands can then change that sub-process' environment, but it can never affect the environment of the processes above it. It would be a big security risk, for one thing.

So in the above example, the script runs in its own subshell, so it starts with a copy of the first shell's pwd, which is then changed to xxx by the cd command. But when the script exits, you're back in the starting shell, which never had its pwd touched.

macemoneta's solution involves sourcing the script, rather than running it. Instead of executing the script as a sub-process, it sucks the commands it contains into the current shell's environment and executes them there.

Depending on what your actual purpose is, another option would be to put your commands inside a shell function instead of a stand-alone script. Since functions sit inside the current environment, the commands they run can affect it directly.

http://mywiki.wooledge.org/BashGuide...ands#Functions

Code:
change_dir() {
	cd xxx
	pwd
}
Then anytime you issue the change_dir command, it will cd the shell into the xxx directory. Put the function inside your bashrc or a similar start-up file if you want to make it permanent (your start-up files are automatically sourced when the shell starts).


PS: The directories used by pushd/popd/dirs can only be directly added to or removed by those commands, although you can also make direct alterations to existing entries in the DIRSTACK array variable. See the bash manpage entries for each of these commands. And no, root permission is not a factor in manipulating them. The su/sudo commands also fork off new subprocesses, so anything done as root will likewise only affect the root user's environment, leading to the same situation as above.

Last edited by David the H.; 09-18-2011 at 05:38 AM. Reason: some restructuring and addendum
 
Old 09-18-2011, 08:26 AM   #5
LloydRice
Member
 
Registered: Jun 2011
Location: Colorado
Distribution: Debian and Ubuntu
Posts: 67

Original Poster
Rep: Reputation: Disabled
Thanks David for the additional information.

What I'm trying to do is to write code (a process, function, script, ..?) which can take me to another place. For now, I'm using macemoneta's solution, which means that I type
. go xxx
when I want to use that system. The trick is that the code interprets 'xxx'. It is not just a directory name. This works, but it would be great to not have to remember to type the '.' in front. I will continue to explore. Thanks.

Lloyd
 
Old 09-19-2011, 09:12 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
Since a function is basically a script that exists inside your current shell environment, whatever you'd put into the file can usually go into the function brackets with very little modification.

Usually about the only changes you have to worry about are ensuring that any variables are scoped properly (usually by declaring them as local first), and that the function name doesn't conflict with any other commands.

If you would explain exactly what you're trying to do, and posted whatever code you have, I'm sure we could help you set it up.
 
Old 10-01-2011, 09:56 PM   #7
LloydRice
Member
 
Registered: Jun 2011
Location: Colorado
Distribution: Debian and Ubuntu
Posts: 67

Original Poster
Rep: Reputation: Disabled
Thank you, David the H., for your very helpful thoughts. I have since explored functions and have learned a lot (and solved the original problem in a very clean way).
 
  


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
Changing the password of Directory Manager in Redhat Directory Services sudarshan.bisht Linux - Server 2 11-19-2013 04:33 AM
Auto Executing script while changing to a directory p_s_shah Linux - Server 5 11-14-2008 06:52 AM
Need script for changing DIRECTORY names only to upper case PaxRomana Programming 15 05-08-2007 05:04 AM
changing directory in bash (or other) script zefram Linux - General 2 09-16-2006 10:41 AM
script for changing a specific string in a directory irfanhab Programming 5 06-28-2005 01:27 PM

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

All times are GMT -5. The time now is 07:23 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
Open Source Consulting | Domain Registration