Using \"cd\" in a bourne shell script

Liam R. E. Quin lee at sq.sq.com
Sat Nov 11 16:35:12 AEST 1989


John Gayman <john at wa3wbu.uucp> wrote:
>>   I'm trying to use "cd" in a bourne shell script
>> [...] I end up in the same directory I started from. 

danl at midget.towson.edu writes:
>you could execute it as:
>	$ . script_name

If you are using the System V shell (or SunOS /bin/sh these days), you
could also define a shell function:
	start(){
	    date
	    who
	    cd /users
	}
Put this -- and any others you invent -- in a standard place, such as
/usr/local/lib/sh-functions,
make it executable with chmod +x, and add
	if [ -x /usr/local/lib/sh-functions ]
	then
	    . /usr/local/lib/sh-functions
	fi
into either /etc/profile (at the end) or the users' own .profiles.

Then they don't need to type the dot.

This question seems to come up a lot, but this particular (somewhat
pragmatic) solution seems not to be suggested often, so I have posted it here.


Lee
-- 
Liam R. Quin, Unixsys (UK) Ltd [note: not an employee of "sq" - a visitor!]
lee at sq.com (Whilst visiting Canada from England)
lee at anduk.co.uk (Upon my return to England at Christmas)



More information about the Comp.unix.questions mailing list