cd from a Bourne Shell Script - Please Help

Dear Bourne Shell Expert,

I am trying to change the current working directory from within a Bourne Shell script. Simply enough i thought !

As I am sure you are well aware, Inside the script i echo `pwd` and it seems ok, but the shell spawns another shell to execute this and as such, when my script finishes it returns to the directory where i executed it in the first place.

i execute the script from /opt/software and simply want to jump to a given directory.

#!/usr/bin/sh

echo " Now in `pwd` ### /opt/software
cd /opt/skill
echo " Now in `pwd` ### /opt/skill

$PROMPT @ /opt/software > pwd returns /opt/software

                                                      but i want it to be /opt/skill !!!!!!

Please accept my mmost profound gratitude for your help support and valuable time in this matter.

Best Regards,

FMA

That's how it's supposed to work. When you execute a shell script, it spawns a sub-process. When you change directory in a subprocess, it may not modify it's parent's PWD, so when it exits, you're right where you began.

If you want to execute a script in the current context, try using a function. For example, in sh you should be able to place something like this in your .profile:

cd_logs(){
 cd /var/log
 pwd
}

Next time you log in (or source your .profile again) you can type cd_logs, and you'll end up in /var/log.

See cd in shell script
and using alias...

Dear all,

Im sorry to keep going on about this but i honestly do need to make a bourne shell script change the working directory of the current shell.

Is there really no simple switch to enable this operation ???

I will add a quick example to illustrate :

#################################################
#!/usr/bin/sh

echo "1. Starting Project Script from = `pwd`" ### /home/fawqati
echo "2. Please Enter Project Name : " ### vega
project_name=readline
echo " Entering Project /des/proj/$project_name " ### /des/proj/vega
cd /des/proj/$project_name

##################################################

After this executes i am still in /home/fawqati and NOT
in /des/proj/<project_name>

This is a real problem and i would be extremely gratefull for some help.

Sincerly

FMA

While I don't understand exactly why you need a script to issue a cd command, why just not create an alias? As in

 alias 'cdv=cd /des/proj/$1'

then you'll be able to type cdv vega - or whatever project you have in /des/proj - and be taken to /des/proj/vega.

Its because i use readline to ask the user which project he want to log into ! This is one one section of a huge modular script i am writing.

Well, don't use readline to ask the user where he wants to go, then.

I have no idea what other things your huge modular script does, but whatever it does, it'll all go away once it exits - other than output redirected to a file, deleted/created files, and such - just like the cd command.

The best I can give you is:

1.- create a user for each project, so users can su to it and be in the right directory, with the right environment, etc.

2.- Use expect. Expect has an "interact' command which gives the user control of the shell back within the script.

If you read the thread you will see that the question relates to bourne shell, bourne shell does not have aliases.

Do you want the user to exit your script and be in the correctory directory?

Then source your script. ie., (note the leading dot)

. /path/to/script.sh

If you are invoking shells ie.,
the first line is

#!usr/bin/sh

then this idea will not work because everytime the user calls the script he execs another shell. Get rid of it. In a sourced script you also should not call exit at the end of the script.

If you read the title you will see that the question relates to a bourne shell SCRIPT. I suggested an alias in the users' environment, not in the script. I have no idea what other users use as login shells. Why would I suggest set an alias in a script to begin with, especially when, even if the script was ksh or csh, it would do what he wants???

Hi Jim,

BTW Great advice, thank you.

So if i understand you right you right, i will end up with a pretty complex .cshrc file. Which is fine by me !

The fact is i just thought there might be a simple way to get control of the shell where the script was initiated. I guess NOT !

Cheers Jim and to everyone for your kind support and valuable time.

FMA