Results 1 to 7 of 7
In bash:
I know how to write a script as a login function that does a CD and "percolates up" to the "calling shell". Is there anyway to do this ...
- 09-12-2007 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 5
bash script file percolate cd up to calling shell
In bash:
I know how to write a script as a login function that does a CD and "percolates up" to the "calling shell". Is there anyway to do this in a script file so that the cwd (current working directory) is the last cwd of the called script?
Note, I don't want to use the source builtins: 'source' and '. ' in the calling shell, and near as I can tell they can't be invoked from within the called shell script file, as in:
. (
... called script
)
I sometimes use the above kind of structure for scripts that I want to execute asynchronously to the shell prompt, as in:
(
... otherwise synchronous script
) &
but it doesn't work with source.
I've also tried 'export PWD' as the last statement in the called script, and that doesn't work either.
TIA,
Lee
- 09-12-2007 #2Just Joined!
- Join Date
- Sep 2007
- Posts
- 5
One man's cold broccoli is another man's moby hack! ;-)
jozyba,
You're "terrible hack" fit the bill perfectly because it not only works, but also makes clear to a reader of the "calling" script that the working directory is going to change based on what happens in the called script.
I've actually used a technique similar to this (where the stdout of the script actually reports back to the calling script with some important side-effect information) before but for a completely different purpose.
10Q>>much!,
Lee
P.S. OBTW, you've raised the level of answers I've seen here by 3 standard deviations in one swell foop!
- 09-12-2007 #3Just Joined!
- Join Date
- Aug 2007
- Posts
- 37
Ooops, sorry! I had second thoughts about my "horrible hack" and thought that I had deleted it before anyone had seen it. Anyway I'd better repost it now so that anyone else reading this thread knows what we're talking about......

*****************************************
I'm not sure that I've understood your question correctly. Do you mean something like this:
The current working directory of calling shell is Directory-A
The calling shell runs a script
The script changes directory to Directory-B, then exits
You want the current working directory of the calling shell to now be Directory-B
If that's what you want to do then "officially" it can't be done because the script (child process) can't export its environment to its parent process (the calling shell).
You can simulate passing the child's $PWD back to the parent by means of a really horrible hack. Say that the_script.sh did this:
Then your shell could call it like this:Code:#! /bin/bash cd / cd $HOME/Desktop echo $PWD exit 0
So the cd command would receive the output from the script's 'echo $PWD' command. The problem with this is that if the_script.sh has more than one 'echo' command in it then the output of all the echo commands will be passed to 'cd':Code:cd "$(bash the_script.sh)"
The 'cd' command would choke on all that echoed output and would throw an error. So, to make a horrible hack even worse, you need to make sure that 'cd' only receives the last string that was echoed:Code:#! /bin/bash cd / echo "I'm now in the $PWD directory" cd $HOME/Desktop echo "And now I'm in the $PWD directory" echo $PWD exit 0
Now the echoed output can be extracted like so:Code:#! /bin/bash cd / echo "I'm now in the $PWD directory" cd $HOME/Desktop echo "And now I'm in the $PWD directory" echo xxx$PWD # Put 'xxx' or some other unique string in front of $PWD exit 0
Code:cd $(x="$(bash the_script.sh)"; echo "${x##*xxx}")
- 09-14-2007 #4Just Joined!
- Join Date
- Sep 2007
- Posts
- 2
easier
Dag. You guys are making it too complicated.
Here, this is easier.
script (saved as cdbobdir, somewhere in your path):
the, you just run this:Code:#!/bin/bash cd /dir/you/want/to/go/to/bobdir pwd
and you're there. The backticks execute the code inline and make cd use the output of cdbobdir.Code:cd `cdbobdir`
- 09-15-2007 #5Just Joined!
- Join Date
- Aug 2007
- Posts
- 37
stuporglue, I don't think you've fully grasped the problem.
Your solution:
Is the same as my first solution:script (saved as cdbobdir, somewhere in your path):
the, you just run this:Code:#!/bin/bash cd /dir/you/want/to/go/to/bobdir pwd
and you're there. The backticks execute the code inline and make cd use the output of cdbobdir.Code:cd `cdbobdir`
However, as I said earlier in this thread, if the script contains more than one echoed output:You can simulate passing the child's $PWD back to the parent by means of a really horrible hack. Say that the_script.sh did this:
Then your shell could call it like this:Code:#! /bin/bash cd / cd $HOME/Desktop echo $PWD exit 0
So the cd command would receive the output from the script's 'echo $PWD' command. The problem with this is that if the_script.sh has more than one 'echo' command in it then the output of all the echo commands will be passed to 'cd'.Code:cd "$(bash the_script.sh)"
Then that solution will not work:Code:#! /bin/bash cd / echo "I'm now in the $PWD directory" cd $HOME/Desktop echo "And now I'm in the $PWD directory" echo $PWD exit 0
Gives an error message:Code:cd `cdbobdir`
And that brings us back to my "horrible hack"....Code:bash: cd: I'm: No such file or directory
- 09-15-2007 #6Just Joined!
- Join Date
- Sep 2007
- Posts
- 2
It's true that only your script will work if you are printing out other information in the mean time, but if you're writing a script which is to be used in the manner suggested (to change directories), why have it output something to the terminal?
To the original poster: If you're just trying to write a command to change to a deep directory, you might just want to use an alias in your ~/.bashrc file. eg.
alias cddeep="cd /home/steve/Documents/school/CS104/homework/project1/code"
- 09-16-2007 #7Just Joined!
- Join Date
- Sep 2007
- Posts
- 5
StuporGlue, if Only Bush had followed your lead
and adopted the moniker BushLeague.


Reply With Quote